#===============================================================================
# I create many-to-many relation between A and B through
ab_association table.
# Then I create many-to-many relation between AB and C.
# When I try to break relation between A and B objects I get next
error:
#
# >>> a_obj.b_objects.remove(b_obj)
# >>> session.flush()
#
#        raise exc.DBAPIError.instance(statement, parameters, e,
connection_invalidat
#    ed=is_disconnect)
#    sqlalchemy.exc.IntegrityError: (IntegrityError) update or delete
on table "ab_as
#    sociations" violates foreign key constraint
"ab_c_associations_ab_id_fkey" on ta
#    ble "ab_c_associations"
#    DETAIL:  Key (id)=(3) is still referenced from table
"ab_c_associations".
#     'DELETE FROM ab_associations WHERE ab_associations.a_id = %(a_id)
s AND ab_assoc
#    iations.b_id = %(b_id)s' {'b_id': 3L, 'a_id': 3L}
#
# How can I use "cascade='delete'" for delete relation between AB and
C and avoid this error?
#
# Sorry for my English :(
#===============================================================================


from sqlalchemy import *
from sqlalchemy.orm import *


uri = 'postgres://postgres:passw...@localhost:5432/db'
engine = create_engine(uri, convert_unicode=True)
metadata = MetaData(engine)

a = Table('a', metadata,
    Column('id', Integer, primary_key=True))

b = Table('b', metadata,
    Column('id', Integer, primary_key=True))

c = Table('c', metadata,
    Column('id', Integer, primary_key=True))

ab_associations = Table('ab_associations', metadata,
    Column('id', Integer, primary_key=True),
    Column('a_id', Integer, ForeignKey('a.id')),
    Column('b_id', Integer, ForeignKey('b.id')),
    UniqueConstraint('a_id', 'b_id')
)

ab_c_associations = Table('ab_c_associations', metadata,
    Column('id', Integer, primary_key=True),
    Column('ab_id', Integer, ForeignKey('ab_associations.id')),
    Column('c_id', Integer, ForeignKey('c.id')),
    UniqueConstraint('ab_id', 'c_id')
)


class A(object): pass
class B(object): pass
class C(object): pass
class AB(object): pass


mapper(A, a)

mapper(B, b, properties={
    'a_objects': relation(A, secondary=ab_associations,
backref='b_objects')})

mapper(AB, ab_associations)

mapper(C, c, properties={
    'ab_objects': relation(AB, secondary=ab_c_associations,
backref='c_objects')})


session = create_session()
metadata.create_all()


a_obj = A()
b_obj = B()
c_obj = C()

session.add(a_obj)
session.add(b_obj)
session.add(c_obj)

a_obj.b_objects.append(b_obj)

session.flush()

ab_obj = session.query(AB).filter_by(a_id = a_obj.id,
b_id=b_obj.id).all()[0]

ab_obj.c_objects.append(c_obj)

session.flush()

a_obj.b_objects.remove(b_obj)

session.flush()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to