I have an association object:
class FolderUserAccess(db.Model):
__tablename__ = "folderuseraccess"
folder_id = db.Column(db.Integer, db.ForeignKey('node.id'),
primary_key=True)
folder = db.relationship('Folder', back_populates='access')
user_id = db.Column(db.Integer, db.ForeignKey('user.id'),
primary_key=True, index=True)
user = db.relationship('User')
access = db.Column(TINYINT, nullable=False)
walk = db.Column(db.Boolean, nullable=False)
When the object is persisted to the database, I would like to delete the
corresponding row when access==0 and walk = False.
Here is my attempt to make this happen:
def add_or_remove(self):
"""Add or remove object depending on access and walk values
ensuring no empty records are stored in database"""
if self.access or self.walk:
if self in db.session.deleted:
db.make_transient(self)
else:
db.session.add(self)
else:
if inspect(self).persistent:
db.session.delete(self)
I call this on the object after updating it. It is complicated as it has to
deal with the fact that somewhere else in the same transaction the object
may have already been updated. It does not work (I will figure it out
eventually) and strikes me as the complicated way of doing things.
What I would really like to do is be able to write a custom implementation
for when the object gets flushed. Something like:
if self.access or self.walk:
#insert or update the row
else:
# delete the row
Is this possible? Or does anyone have any great ideas on how to achieve the
same result in a more straightforward way than my current approach.
Thanks,
Richard
--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.