set default engine options while using SQLALCHEMY_BINDS
When engine options is not specified in <code>SQLALCHEMY_BINDS</code>, this engine will lack options with the bind. Which may cause some unexpected errors such as database disconnect error.
<pre>
SQLALCHEMY_BINDS = {"main": "mysql+pymysql://root:root@localhost:3306/dbname"}
SQLALCHEMY_ENGINE_OPTIONS = {"pool_pre_ping": True}
</pre>
For example, if someone use Flask-SQLalchemy like this, he will think <code>SQLALCHEMY_ENGINE_OPTIONS</code> will make effect in <code>SQLALCHEMY_BINDS</code>. But that's not the case.
So I suggest use <code>SQLALCHEMY_ENGINE_OPTIONS</code> as default engine options specifically when engine options is not specified in <code>SQLALCHEMY_BINDS</code>. So the code above will be same as
<pre>
SQLALCHEMY_BINDS = {
"main": {
"url": "mysql+pymysql://root:root@localhost:3306/dbname",
"pool_pre_ping": True
}
}
</pre>
关闭于 2025-08-05 1 条评论