Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models模块中是否适合提交数据库会话 #216

Open
yuxiaoy1 opened this issue Jul 19, 2020 · 2 comments
Open

models模块中是否适合提交数据库会话 #216

yuxiaoy1 opened this issue Jul 19, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@yuxiaoy1
Copy link
Contributor

参考Four SQLAlchemy Tips,Miguel建议不要在models模块中使用db.session.commit(),而应该在更高层的业务逻辑代码中去使用它(比如视图函数中),理由是可能会出现不符合预期的结果。
想了解下辉哥你对于这个问题的看法是什么,因为目前狼书里相关项目的models模块是包含db.session.commit()的,想问下根据你的经验,这种写法有出现过问题吗?

@yuxiaoy1
Copy link
Contributor Author

yuxiaoy1 commented Aug 9, 2020

查了下,发现SQLAlchemy docs中也提到了这点:https://docs.sqlalchemy.org/en/13/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it,所以best practice应该是models模块中不使用commit提交会话,转而在更高层的业务逻辑代码中使用。

@greyli
Copy link
Owner

greyli commented Sep 17, 2020

我倒没遇到实际问题,既然文档提到了,我考虑在第二版更新下。谢谢!

@greyli greyli added enhancement New feature or request and removed discussion labels May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants