On Apr 1, 2008, at 5:59 PM, Bobby Impollonia wrote:
>
> This fails (as expected) because of the circular dependency. Before I
> switched to declarative, I had solved this by using a
> ForeignKeyConstraint with use_alter.
> I can't see how to do that with declarative. I tried changing Node2
> to:
ForeignKey takes the use_alter argument as well:
class Node2(Base):
__tablename__ = 'node2'
id = Column('id', Integer, primary_key=True)
parent_node1_id = Column('parent_node1_id', Integer,
ForeignKey('node1.id', use_alter=True, name='foo'))
>
> Also, I think that I have to put the relations outside of the class
> definitions (as above) because the primaryjoin arguments references
> the class itself. Is there a way around this?
One way might be to go half the "old" route and create Table objects
before you define the classes, setting them on the declarative using
"__table__". then you'd use the table objects for the expressions.
This is kind of the reason I've been into datamapper for so long.
But, you can actually get pretty wacky with the combination of mapper
and declarative. check this one out:
node1_id = Column('id', Integer, primary_key=True)
node2_id = Column('id', Integer, primary_key=True)
class Node1(Base):
__tablename__ = 'node1'
id = node1_id
parent_node2_id = Column('parent_node2_id', Integer,
ForeignKey('node2.id'))
parent_node2 = relation("Node2", primaryjoin = node2_id ==
parent_node2_id, backref = 'children')
class Node2(Base):
__tablename__ = 'node2'
id = node2_id
parent_node1_id = Column('parent_node1_id', Integer,
ForeignKey('node1.id', use_alter=True, name='foo'))
parent_node1 = relation("Node1", primaryjoin = node1_id ==
parent_node1_id, backref = 'children')
metadata.create_all()
I don't know if the above is the best way to do this but it is very
enjoyable.
Theres various ways, using callables or eval'ed strings, that it can
be completely "inlined" even for a bi-directionally-dependent case
like this one, but I'm not really sure they're worth it. The point of
declarative is to simplify the large majority of *simple* mappings.
If you have a complicated one like this, you have to drop into mapper
concepts a little bit.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---