Re: [sqlalchemy] Is it possible to add another criterion to this backref?
Is what I'm trying to be possible assuming I cannot add any code to the User model? In the future there might be plugins in my application which could contain favorites, but while plugins can add their own models, they are never allowed to directly modify a class in the application core. -- 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
In case it's unclear what exactly I'm trying to do, here's the version with the relationship defined right in the User model that works fine. I'd like to do this exact same thing, but somehow define the relationship outside the User model. Preferably by using the normal declarative syntax to define the association table and defining the relationship there. # in the User model: favorite_users = db.relationship( 'User', secondary=favorite_user_table, primaryjoin=id == favorite_user_table.c.user_id, secondaryjoin=(id == favorite_user_table.c.target_id) ~is_deleted, lazy=True, backref=db.backref('favorite_of', lazy=True), ) # the association table: favorite_user_table = db.Table( 'favorite_users', db.metadata, db.Column( 'user_id', db.Integer, db.ForeignKey('users.users.id'), primary_key=True, nullable=False, index=True ), db.Column( 'target_id', db.Integer, db.ForeignKey('users.users.id'), primary_key=True, nullable=False ), schema='users' ) -- 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
Im trying to avoid having to write a full example for you from scratch so if you could provide everything in one example, both models and where you want the relationships, with all the columns, we can work from there, thanks. Adrian adr...@planetcoding.net wrote: In case it's unclear what exactly I'm trying to do, here's the version with the relationship defined right in the User model that works fine. I'd like to do this exact same thing, but somehow define the relationship outside the User model. Preferably by using the normal declarative syntax to define the association table and defining the relationship there. # in the User model: favorite_users = db.relationship( 'User', secondary=favorite_user_table, primaryjoin=id == favorite_user_table.c.user_id, secondaryjoin=(id == favorite_user_table.c.target_id) ~is_deleted, lazy=True, backref=db.backref('favorite_of', lazy=True), ) # the association table: favorite_user_table = db.Table( 'favorite_users', db.metadata, db.Column( 'user_id', db.Integer, db.ForeignKey('users.users.id'), primary_key=True, nullable=False, index=True ), db.Column( 'target_id', db.Integer, db.ForeignKey('users.users.id'), primary_key=True, nullable=False ), schema='users' ) -- 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. -- 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.
Re[2]: [sqlalchemy] Is it possible to add another criterion to this backref?
Sure, no problem with that. I'll add a small self-contained example for it tomorrow. - Adrian On 25.03.2015 14:21 Michael Bayer wrote: Im trying to avoid having to write a full example for you from scratch so if you could provide everything in one example, both models and where you want the relationships, with all the columns, we can work from there, thanks. -- 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
The is_deleted column is in the User table. If possible I'd rather avoid having to replicate it in the favorite tables (hard-deleting favorites is fine, I only need soft deletion for users). -- 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.
[sqlalchemy] Is it possible to add another criterion to this backref?
Hi, I have the following models for a favorite system: https://gist.github.com/ThiefMaster/e4f622d54c74ee322282 Now I'd like to restrict the relationship that's created by the backref in L24, so it doesn't include any favorited users which have the is_deleted column set to true. I tried playing around with primaryjoin in the backref but couldn't get it working. Is what I'm trying to do actually possible? Cheers 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
ThiefMaster adr...@planetcoding.net wrote: The is_deleted column is in the User table. If possible I'd rather avoid having to replicate it in the favorite tables (hard-deleting favorites is fine, I only need soft deletion for users). the column can be on either side. I think maybe you want to illustrate the actual attempts that aren’t working if there’s some kind of help being sought here. -- 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. -- 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
@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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
Adrian adr...@planetcoding.net wrote: @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' ) ) the primaryjoin should be either the expression as a Python object (not a string), or a lambda that returns the expression object (not a string), or if you’re using declarative it can be a string that’s eval’ed. But not a lambda *and* a string at the same time. 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’ yeah that’s the lambda + string together which is not the correct use. 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). the model here doesn’t illustrate how this would be a many-to-many. -- 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.
Re: [sqlalchemy] Is it possible to add another criterion to this backref?
ThiefMaster adr...@planetcoding.net wrote: Hi, I have the following models for a favorite system: https://gist.github.com/ThiefMaster/e4f622d54c74ee322282 Now I'd like to restrict the relationship that's created by the backref in L24, so it doesn't include any favorited users which have the is_deleted column set to true. I tried playing around with primaryjoin in the backref but couldn't get it working. Is what I'm trying to do actually possible? yes, if this had an is_deleted column (which it does not) you’d add that to the primaryjoin (which I don’t see here), it would only select for that column. Cheers 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. -- 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.