Closed Jackstrawcd closed 1 week ago
报错信息为 (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)") (Background on this error at: https://sqlalche.me/e/14/e3q8),说明 alembic 在执行迁移文件时访问的是 localhost
修改 alembic.ini 将 sqlalchemy.url 中数据库信息改为和 .env 文件中 DB_CONNECTION 内容一致即可
描述
执行数据迁移脚本使用下面的命令:
显示
执行迁移文件完成
然而数据库中并不存在。输出
过程中控制台会打印:
环境
额外信息
.env
文件中DB_CONNECTION
配置为远程数据库。修改alembic/env.py
需要配置数据库信息而非使用默认的localhost
:from logging.config import fileConfig
from sqlalchemy import engine_from_config from sqlalchemy import pool
from alembic import context from core.config import DATABASE_URL from db.base import Base
把当前项目路径加入到path中
sys.path.append(os.path.dirname(os.path.dirname(file)))
this is the Alembic Config object, which provides
access to the values within the .ini file in use.
config = context.config
Interpret the config file for Python logging.
This line sets up loggers basically.
fileConfig(config.config_file_name)
add your model's MetaData object here
for 'autogenerate' support
from myapp import mymodel
target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
other values from the config, defined by the needs of env.py,
can be acquired:
my_important_option = config.get_main_option("my_important_option")
... etc.
修改数据库地址
config.set_main_option('sqlalchemy.url', str(DATABASE_URL))
.......