Create a transaction save point for multiple commits #1192
-
I am working on a project and currently using flask-sqlalchemy library. Everything seems to work fine until I attempt to create a transaction save point, I am pretty sure am doing it wrongly, but I can't just figure out why. I would like to make several saves which are dependent on each other, however, I need to rollback all changes if even one fails. How do I go about this, below is my current function.
Running this return I have tried using How can I rollback multiple commits using flask-sqlalchemy. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
After much research, I found a way to go about it, please let me know if there are any issues with my process. so, rather than having to start a new session and commit each change, I instead flush to get the id I need to perform the following action. So instead of what I have above, I have this.
|
Beta Was this translation helpful? Give feedback.
-
You should not use IDs to link related objects. If you assign the actual objects to the relationship attributes, sqlalchemy will automatically populate the FKs during flush. user = User(...)
db.session.add(user) # only needed if none of the objects are already in the DB (or rather session)
profile = Profile(user=user)
wallet = Wallet(profile=profile)
db.session.commit() |
Beta Was this translation helpful? Give feedback.
After much research, I found a way to go about it, please let me know if there are any issues with my process.
so, rather than having to start a new session and commit each change, I instead flush to get the id I need to perform the following action. So instead of what I have above, I have this.