SQLAlchemy SQLite URL相对于家庭或环境变量

sqlite数据库的相对sqlalchemy路径可以写为:

sqlite:///folder/db_file.db

绝对是:

sqlite:////home/user/folder/db_file.db

是否可以写一个相对于家的路径?像这样:

sqlite:///~/folder/db_file.db

或更妙的是,路径可以包含环境变量吗?

sqlite:////${MY_FOLDER}/db_file.db

这是alembic.ini文件的上下文。因此,如果先前的目标无法直接实现,我是否可以使用变量替换作弊?

[alembic]
script_location = db_versions
sqlalchemy.url = sqlite:///%(MY_FOLDER)s.db
...
bigege 回答:SQLAlchemy SQLite URL相对于家庭或环境变量

来自alembic documentation(重点是我):

  

sqlalchemy.url-通过SQLAlchemy连接到数据库的URL。仅当env.py文件调用它们时才使用此配置值;在“通用”模板中,在run_migrations_offline()函数中对config.get_main_option(“ sqlalchemy.url”)的调用以及在run_migrations_online()函数中对engine_from_config(prefix =“ sqlalchemy。”)的调用是此键所在的位置参考。 如果SQLAlchemy URL应该来自其他来源,例如环境变量或全局注册表,或者如果迁移环境使用多个数据库URL,则鼓励开发人员 env.py 文件以使用适当的任何方法来获取数据库URL。

因此,在这种情况下,sqlalchemy url格式可以被python本身规避和生成。

,

我通过在 config 导入后修改 env.py 对象中的值解决了这个问题:

# this is the Alembic Config object,which provides
# access to the values within the .ini file in use.
config = context.config

# import my custom configuration
from my_app import MY_DB_URI
# overwrite the desired value
config.set_main_option("sqlalchemy.url",MY_DB_URI)

现在 config.get_main_option("sqlalchemy.url") 返回您想要的 MY_DB_URI

本文链接:https://www.f2er.com/3005570.html

大家都在问