Hi, I have 3 classes: two have many to many relationship between them, one
is association class.
class Person:
...
tools = relationship('Association', back_populates='user', lazy=True,
cascade='all, delete-orphan')
class Tool:
...
users = relationship('Association', back_populates='tool', lazy=True,
cascade='all, delete-orphan')
One person object has 3 tools [<Tool id=1>, <Tool id=2>, <Tool id=3>]. When
I update the tool of the person I did
person.tools = []
for tool_id in [2, 4, 5]:
tool = Tool.query.get(tool_id)
if tool:
person.tools.append(tool)
This operation fails, since when I set tools to an empty list, tool 1,2,3
are deleted, so only tool 4,5 are added into the list. If I remove the
delete-orphan in the cascade, I will have some redundant data in the
database. I can remove the unwanted tools from person.tools first instead
of setting it to empty list. I'd like to know if there is a simple way to
achieve same result. Thank you.
--
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.