On May 24, 2010, at 10:38 PM, chrysn wrote:

> hello sqlalchemy,
> 
> i'm setting up a data structure that builds a group of trees using
> combined primary keys. i got it to work (with help from rmancy on irc),
> but it is far from intuitive.
> 
> my code looks like this:
> 
> ql =  Table('ql', metadata,
>            # that's not the original column sort order, but it shouldn't 
> matter
>            Column(u'qid', Integer, primary_key=True),
>            Column(u'lang', String(5), primary_key=True),
>            Column(u'version', Integer, primary_key=True),
>            Column(u'reflang', String(5), ForeignKey('ql.lang')),
>            Column(u'refversion', Integer, ForeignKey('ql.version')),
>            Column(...),
>    )
> 
> 
> class QL(object):
>    def __repr__(self):
>        return '<QL (%s, %s, %s)>'%(self.qid, self.lang, self.version)
> 
> 
> mapper(QL, ql, properties={
>        ...,
>        'ref': relationship(
>            QL,
>            primaryjoin=and_(
>                ql.c.qid==ql.c.qid,
>                ql.c.reflang==ql.c.lang,
>                ql.c.refversion==ql.c.version),
>            backref=backref('backref'),
>            foreign_keys=[ql.c.qid, ql.c.reflang, ql.c.refversion],
>            remote_side=[ql.c.qid, ql.c.lang, ql.c.version]),
>    })
> 
> my problem is that the primaryjoin query is rather unintellegible --
> which side is which? i tried to substitude the right sides of the
> primaryjoin and the remote_side items with referenced_ql =
> ql.alias('referenced_ql'), but that made the query construction fail
> with "Could not locate any equated, locally mapped column pairs for
> primaryjoin condition ...". 
> 
> is there any way to express this more clearly?

this is a very rare use case, in that your primary join requires the equation 
of a column to itself.   So the hint provided by remote_side is required.   The 
foreign keys and primaryjoin hints should not be needed here, however.     
There are examples of various varieties of configuration for this type of join 
in http://www.sqlalchemy.org/trac/browser/test/orm/test_relationships.py#L104 . 
 You should be able to use the style in `test_very_implicit`, i.e. only 
remote_side on the many-to-one side of the relationship().   

-- 
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.

Reply via email to