Hi there,

I'm having an issue when building queries against a relationship using a 
custom primaryjoin.
The issue can be reproduced with a slightly modified Users/Address model as 
taken from the docs:

from sqlalchemy import Integer, ForeignKey, String, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)

    address_id = Column(Integer, ForeignKey('address.id'))
    boston_address = relationship("BostonAddress",
                                  
primaryjoin="and_(User.address_id==Address.id, "
                                              "Address.city=='Boston')")

class Address(Base):
    __tablename__ = 'address'
    __mapper_args__ = {'polymorphic_on': 'type',
                       'polymorphic_identity': 'any'}

    id = Column(Integer, primary_key=True)
    type = Column(String, primary_key=True)

    street = Column(String)
    city = Column(String)
    state = Column(String)
    zip = Column(String)

class BostonAddress(Address):

    __mapper_args__ = {'polymorphic_on': 'type',
                       'polymorphic_identity': 'Boston'}


The actual application uses more than one Address subclass obviously - but 
the above suffices for the example.
When trying to query for "Users not having a boston_address" I'd use

User.boston_address == None

as a where clause, which however turns out be become:

"user".address_id IS NULL AND 'Boston' IS NULL

which obvisouly won't yield any results - I'm not sure though, whether this 
is a bug or simply due to the maybe unusual model.
I've now implemented a dedicated comparator for this type of relationship 
which seems to work.
But still I'm wondering if there's an easier way to fix this, e.g. by 
annotating parts of the join condition as "do not touch"...?

Cheers
Sebastian



-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to