Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Last active November 25, 2021 10:40
Show Gist options
  • Save michaelbukachi/7554cd575b24092b8ac92b2c0bacefe7 to your computer and use it in GitHub Desktop.
Save michaelbukachi/7554cd575b24092b8ac92b2c0bacefe7 to your computer and use it in GitHub Desktop.
Flask-Admin with `autocommit=True`
'''
This workaround helps avoid:
sqlalchemy.exc.InvalidRequestError: No transaction is begun
whenever you try to create/update a model when `autocommit=True`
'''
class BaseModelView(ModelView):
def create_model(self, form):
self.session.begin()
super(BaseModelView, self).create_model(form)
def update_model(self, form, model):
self.session.begin()
super(BaseModelView, self).update_model(form, model)
def delete_model(self, model):
self.session.begin()
super(BaseModelView, self).delete_model(model)
# Rest of the code
@Dav3whit3
Copy link

Hello,

I'm facing the same problem with autocommit + and Flask-admin explicit session-commits, but with your code I'm still getting the same "No transaction is begun" error. If I paste my code, would you be able to take a look at it?:)

Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment