Sorry for the delay. Yes, i want to reference Table22, the 'correct'
code is as follows, but obtains the same result. Don't understand
because not run when it corrects.. Is it not possible to link
TableExt22 with Table22 only?
Thanks for the quick answer!
Fernando
#-------------------------------------------------------------------------
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)
On 18 mayo, 18:00, Michael Bayer <[email protected]> wrote:
> On May 18, 2009, at 8:51 AM, fleong wrote:
>
>
>
> > 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)
>
> >>> [0]
> > <class '__main__.Table21'>
> > <class '__main__.Table22'>
>
> TableEx22.t22, despite the name, references the Table2 class. You
> probably want that to reference Table22. the result is still strange,
> but this whole program contains typos and doesn't even run when they
> are corrected, so for further help you'd have to post the actual test
> case.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---