Hi Everybody,
New to the forum.
I'm having a problem setting up a relation. In this hypothetical
example, I have a tree with one parent having many children, and each
child having many attributes. I want to create a relation where each
Parent has a list of all attributes that any of it's children have.
This relation works fine using 0.3.4, but breaks with the following
error with 0.3.7 when I try to instantiate a Parent object:
sqlalchemy.exceptions.ArgumentError: Cant locate any foreign key
columns in primary join condition 'attribute.child_id = child.id AND
child.parent_id = parent.id' for relationship 'Parent.attributes
(Attribute)'. Specify 'foreign_keys' argument to indicate which
columns in the join condition are foreign.
I tried adding various foreign_keys attributes without much luck; I'm
guessing I just don't know what I'm doing...
Here is the example:
----------------------------------------------------------------------------------
from sqlalchemy import *
metadata = BoundMetaData("sqlite:///relation.db")
metadata.engine.echo = True
session = create_session(bind_to = metadata.engine)
parent_table = Table('parent', metadata,
Column('id', Integer, primary_key=True),
)
child_table = Table('child', metadata,
Column('id', Integer, primary_key=True),
Column('parent_id', Integer, ForeignKey('parent.id')),
)
attribute_table = Table('attribute', metadata,
Column('id', Integer, primary_key=True),
Column('child_id', Integer, ForeignKey('child.id')),
)
class Parent(object):
pass
class Child(object):
pass
class Attribute(object):
pass
parent_mapper = mapper(
Parent,
parent_table,
properties = dict(
children = relation(Child, backref='parent'),
attributes = relation(Attribute,
primaryjoin = and_(attribute_table.c.child_id ==
child_table.c.id, child_table.c.parent_id == parent_table.c.id),
backref = 'parent',
)
)
)
child_mapper = mapper(
Child,
child_table,
)
attribute_mapper = mapper(
Attribute,
attribute_table,
properties = dict(
child = relation(Child, backref=backref('attributes')),
)
)
metadata.create_all()
p = Parent()
----------------------------------------------------------------------------------
Any ideas?
Thanks!
-Patrick
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---