Mike Conley wrote:
> I was getting ready to do something similar, and now I am confused.
>
> How is what GHZ is doing fundamentally different from the documentation
> example "Specifying Alternate Join Conditions to relation()"? Doesn't that
> example wrap the primaryjoin in the and_() construct also?
>
> http://www.sqlalchemy.org/docs/05/mappers.html#specifying-alternate-join-conditions-to-relation
>
>
> I can reproduce GHZ's error with declarative, but also can run the
> documentation example using mappers.
>
> Can we express the alternate joins with declarative?

Here's all the ways we are talking about.

The one in the docs, which uses the mapped Table objects to produce column
expressions:

primaryjoin=and_(users_table.c.user_id==addresses_table.c.user_id,
                addresses_table.c.city=='Boston')

With declarative, you also might say within the User class, where User's
columns are in the current scope (assuming Address were declared first):

primaryjoin=and_(user_id==Address.user_id,
                Address.city=='Boston')

or if you were on the Address class, where Address' columns are in the
current scope (assuming User were declared first):

primaryjoin=and_(User.user_id==user_id,
                city=='Boston')

For the purposes of "who's declared first" not being an issue, declarative
also allows key relation() arguments to be specified as strings, which
represent Python expressions that will be evaluated before being processed
by the mapper configuration.  In this case, it looks like:


primaryjoin="and_(User.user_id==Address.user_id,
                Address.city=='Boston')"

just to be clear: the argument to primaryjoin is a *string*. it is not an
"and_()" construct - that is not a string, its an expression object.  
Arguments inside of and_() need to be further SQL expression constructs. 
If they are sent as strings, and_() coerces them into text() objects which
represent literal SQL constructs.  So the below is invalid as far as
literal SQL:

# invalid!
primaryjoin = and_("User.user_id==Address.user_id", "Address.city=='Boston'")

since to emit that as SQL would look like:

and_("users.user_id=addresses.user_id", "addresses.city='Boston'")

but also doesn't work for the ORM, even if the SQL were valid, which has
no way to parse a text() construct and needs to in this case in order to
learn about the join condition.


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