declarative and __table_args__ I must be missing something simple.

284 views
Skip to first unread message

Wayne Witzel

unread,
Apr 8, 2009, 4:49:24 PM4/8/09
to sqlalchemy
I assume I am over looking some simple thing, but I just can't seem to
find it. Thanks for the assist, I have palms open ready for face
planting.

Using a class and table with orm.mapper()

class Child(object):
pass
child_table = Table('child', meta.metadata,
Column('parent_id', Integer, primary_key=True),
Column('parent_ref', Integer, nullable=False),
Column('content', String(10)),
ForeignKeyConstraint(['parent_id', 'parent_ref'],
['parent.id', 'parent.ref'])
)
orm.mapper(Child, child_table)

class Parent(object):
pass
parent_table = Table('parent', meta.metadata,
Column('id', Integer, primary_key=True),
Column('ref', Integer, primary_key=True)
)
orm.mapper(Parent, parent_table, properties={
'children':relation(Child, lazy=False)
})

Produces the following create

2009-04-08 16:36:54,319 INFO sqlalchemy.engine.base.Engine.0x...a6b0
CREATE TABLE parent (
id INTEGER NOT NULL,
ref INTEGER NOT NULL,
PRIMARY KEY (id, ref)
)
2009-04-08 16:36:54,319 INFO sqlalchemy.engine.base.Engine.0x...a6b0
{}
2009-04-08 16:36:54,454 INFO sqlalchemy.engine.base.Engine.0x...a6b0
COMMIT
2009-04-08 16:36:54,456 INFO sqlalchemy.engine.base.Engine.0x...a6b0
CREATE TABLE child (
parent_id INTEGER NOT NULL,
parent_ref INTEGER NOT NULL,
content VARCHAR(10),
PRIMARY KEY (parent_id),
FOREIGN KEY(parent_id, parent_ref) REFERENCES parent (id, ref)
)

Using what I believe is the exact same thing with declarative produces
the creates minus the composite foreign key and then of course is
unable to establish the relation.

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
ref = Column(Integer, primary_key=True)
children = relation("Child", lazy=False)

class Child(Base):
__tablename__ = 'child'
__table_args__ = ForeignKeyConstraint(['parent_id','parent_ref'],
['parent.id', 'parent.ref'])

parent_id = Column(Integer, primary_key=True)
parent_ref = Column(Integer, nullable=False)
content = Column(String(10))

The create output is

2009-04-08 16:47:08,331 INFO sqlalchemy.engine.base.Engine.0x...a710
CREATE TABLE child (
parent_id INTEGER NOT NULL,
parent_ref INTEGER NOT NULL,
content VARCHAR(10),
PRIMARY KEY (parent_id)
)
2009-04-08 16:47:08,331 INFO sqlalchemy.engine.base.Engine.0x...a710
{}
2009-04-08 16:47:08,464 INFO sqlalchemy.engine.base.Engine.0x...a710
COMMIT
2009-04-08 16:47:08,466 INFO sqlalchemy.engine.base.Engine.0x...a710
CREATE TABLE parent (
id INTEGER NOT NULL,
ref INTEGER NOT NULL,
PRIMARY KEY (id, ref)
)

Wayne Witzel

unread,
Apr 8, 2009, 5:20:41 PM4/8/09
to sqlalchemy
Yep, there it is. Stupidly simple. Dug up some old know working source
that did and diff and grep later I found the cause.

__table_args__ needs to be give a tuple with an empty dictionary, like
so.

__table_args__ = (ForeignKeyConstraint(['parent_id', 'parent_ref'],
['parent.id', 'parent.ref']), {})

Now all is well, sorry for the ML clutter. I am face palming in 3, 2,
1 ....
Reply all
Reply to author
Forward
0 new messages