sqlalchemy 数据库的相对路径可以写为:
sqlite:///folder/db_file.db
绝对的一个是:
sqlite:////home/user/folder/db_file.db
可以写相对于home的路径吗?像这样:
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
...
最佳答案
我通过在 env.py
导入后修改 config
对象中的值解决了这个问题:
# 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
。
关于sqlite - Sqlalchemy sqlite url 相对于 home 或环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59109682/