the foreign key between table2 and their children was wrong.. now works correctly.
Thanks for your help! greetings, Fernando table_21 = \ Table( 'table_21', metadata, Column('id2', Integer, primary_key = True), Column('info', String), ForeignKeyConstraint(['id2'],['table_2.id2']), ) table_22 = \ Table( 'table_22', metadata, Column('id2', Integer, primary_key = True), Column('ext', Integer, nullable = False), Column('info', String), ForeignKeyConstraint(['id2'],['table_2.id2']), ForeignKeyConstraint(['ext'],['table_ex22.id']) ) On 18 mayo, 23:51, Michael Bayer <mike...@zzzcomputing.com> wrote: > try taking a look at your SQL output. the issue is pretty obvious, > the join condition between your two tables is insufficient to get a > T22 row. table_2.id has two rows with "0" for the "id" column, so > joining from table_2 to table_22 on just "id" gives you two rows. it > has nothing to do with the relation() or anything else. > > On May 18, 2009, at 3:59 PM, fleong wrote: > > > from sqlalchemy import * > > from sqlalchemy.orm import * > > > engine = create_engine('sqlite:///',echo = True) > > > metadata = MetaData() > > > table_1 = \ > > Table( > > 'table_1', metadata, > > Column('id', Integer, primary_key = True), > > ) > > > table_2 = \ > > Table( > > 'table_2', metadata, > > Column('id', Integer, primary_key = True), > > Column('id2', Integer, primary_key = True), > > Column('type', Integer, nullable = False), > > ForeignKeyConstraint(['id'],['table_1.id']) > > ) > > > table_21 = \ > > Table( > > 'table_21', metadata, > > Column('id', Integer, primary_key = True), > > Column('id2', Integer, primary_key = True), > > Column('info', String), > > ForeignKeyConstraint(['id'],['table_2.id']), > > ) > > > table_22 = \ > > Table( > > 'table_22', metadata, > > Column('id', Integer, primary_key = True), > > Column('id2', Integer, primary_key = True), > > Column('ext', Integer, primary_key = True), > > Column('info', String), > > ForeignKeyConstraint(['id'],['table_2.id']), > > ForeignKeyConstraint(['ext'],['table_ex22.id']) > > ) > > > table_ex22 = \ > > Table( > > 'table_ex22', metadata, > > Column('id', Integer, primary_key = True), > > ) > > > class Table1(object): > > pass > > > class Table2(object): > > pass > > > class Table21(Table2): > > pass > > > class Table22(Table2): > > pass > > > class TableEx22(object): > > pass > > > mapper(Table1, table_1, > > properties = {\ > > 't2': relation(Table2, backref = 't1', cascade = 'all, delete, > > delete-orphan', passive_updates = False, passive_deletes = False), > > } > > ) > > mapper(TableEx22, table_ex22, > > properties = {\ > > 't22': relation(Table22, backref = 'tex', cascade = 'all, > > delete, delete-orphan', passive_updates =False, passive_deletes = > > False), > > } > > ) > > mapper(Table2, table_2, polymorphic_on=table_2.c.type, > > polymorphic_identity=0) > > mapper(Table21, table_21, inherits=Table2, polymorphic_identity=1) > > mapper(Table22, table_22, inherits=Table2, polymorphic_identity=2) > > > Session = sessionmaker() > > #------------------------------------------------------------------------------ > > > #test: > > > metadata.bind = engine > > metadata.create_all() > > > s = Session() > > > t = Table1() > > t.id = 0 > > > tex = TableEx22() > > tex.id = 0 > > > s.add(t) > > s.add(tex) > > s.commit() > > > t21 = Table21() > > t21.id2 = 0 > > t21.t1 = t > > s.add(t21) > > s.commit() > > > t22 = Table22() > > t22.id2 = 1 > > t22.t1 = t > > t22.tex = tex > > s.add(t22) > > s.commit() > > > for i in tex.t22: > > print type(i) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---