@declared_attr
    def user(cls):
        """The user owning this favorite"""
        return db.relationship(
            'User',
            lazy=False,
            foreign_keys=lambda: [cls.user_id],
            backref=db.backref(
                '_favorite_users',
                lazy=True,
                cascade='all, delete-orphan',
                primaryjoin=lambda: '(User.id == user_id) & 
~target.is_deleted'
            )
        )

I've added it on the backref since that's the relationship where I want the 
filter to apply.
In the end I'd like to be able to do this: 
User.query.get(123)._favorite_users which would get me a list of all the 
favorite users (I'll be using association_proxy, but for now I need to get 
the relationships themselves working) besides those users who have 
is_deleted=True (on the User, not the FavoriteUser).

But no matter what I put there (tried both lambdas and strings), I always 
get this error (so I can't even try to figure out the correct criteria to 
use, since it fails early, during mapper configuration time):
sqlalchemy.exc.ArgumentError: Column-based expression object expected for 
argument 'primaryjoin'; got: '(User.id == user_id) & ~target.is_deleted', 
type <type 'unicode'>


Actually.... looking at this code again... it's almost a standard 
many-to-many relationship, so I should probably be using secondary and 
secondaryjoin somewhere. Can I define this backref-like, i.e. from within 
the FavoriteUser model? That way I don't have to spread things around so 
much (which would be the case if I defined the relationship in the User 
model).

-- Adrian

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to