ITADN

flask-sqlalchemy-lite session handling

#2831Openkesmik 创建于 2026-03-14
bug
K
kesmikcommented
Hello, I hope it is a correct section as a bug, but maybe it is my user error, but any help is appreciated I am testing **flask-admin** with **flask-sqlalchemy-lite** and noticed two issues, IMO both are related to session handling. 1. When `MODE = "lite"` is set trying to create "Art" throws an error: sqlalchemy.exc.InvalidRequestError: Object '<Team at 0x736012735490>' is already attached to session '1' (this is '2') - It could be fixed by uncommenting `form_extra_fields` or `form_args` in `ArtsView`. - Is this expected? - If yes which approach I should use? - Little explanation why it is needed would be awesome. - It works with full sqlalchemy (`MODE = "anything"`) without `form_extra_fields` or `form_args`) 2. When `USE_CUSTOM_EQUATION = False` (You can see base class, it is just implements custom `__eq__` editing model (adding additioanl teams) throws an error that model already exist" It happens because flask-admin Unique validator sees objects in different sessions and default object equal check fails here, because form.__obj and obj are loaded separately: `if not hasattr(form, "_obj") or not form._obj == obj:` **I have created simple project for easy reproduction, hope someone could check and assist:** - https://github.com/kesmik/flask-admin-multi-sessions - clone it and run with `uv run main.py` - try to create and "Art" and it will throw error that Team already exist in different session - uncomment either `form_extra_fields` or `form_args` in ArtsView and it works fine - Try to edit Art (add extra team, not name), Unique validator fails - Change from `USE_CUSTOM_EQUATION = False` to `USE_CUSTOM_EQUATION = True`, editing works - if MODE = "anythong except lite" it will run full sqlalchemy and creating/editing works without changing anything Sadly I am fresh to **sqlalchemy** and **flask-admin**, so little help from my side here, but if anyone points essential problem, I might be able to solve it and provide pull request. Environment: - Python version: 3.14.0 - Flask version: v3.1.3 - Flask-Admin version: github version @ 0a64283914d5a49ab68c09491c7da0c47d02a1b8
3 条评论