python - 失败 : No config file 'alembic.ini' found

标签 python alembic

我尝试在 Alembic 中进行更改,但当我尝试运行 Alembic current 时出现错误。我是 alembic 新手,请告诉我为什么会出现此错误以及如何解决?

我可以在迁移文件夹中看到 alembic.ini 以及 Alembic 使用的修订标识符,一切看起来都很好。

$alembic current
No handlers could be found for logger "alembic.util"
  FAILED: No config file 'alembic.ini' found, or file has no '[alembic]' section

20c921506336_.py:

"""empty message

Revision ID: 20c921506336
Revises: None
Create Date: 2015-01-30 16:28:38.981023

"""

# revision identifiers, used by Alembic.
revision = '20c921506336'
down_revision = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=True),
    sa.Column('email', sa.String(length=50), nullable=True),
    sa.Column('age', sa.Integer(), nullable=True),
    sa.Column('bestfriend_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['bestfriend_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(u'ix_user_age', 'user', ['age'], unique=True)
    op.create_index(u'ix_user_email', 'user', ['email'], unique=True)
    op.create_index(u'ix_user_name', 'user', ['name'], unique=True)
    op.create_table('friends',
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('friend_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['friend_id'], ['user.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
    )
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('friends')
    op.drop_index(u'ix_user_name', table_name='user')
    op.drop_index(u'ix_user_email', table_name='user')
    op.drop_index(u'ix_user_age', table_name='user')
    op.drop_table('user')
    ### end Alembic commands ###

最佳答案

您必须cd 到具有alembic.ini 的目录才能运行alembic 命令,即alembic .ini 必须在当前工作目录中找到;或者您可以使用 alembic -c path/to/alembic.ini 指定配置文件位置。

而且您的 alembic ini 似乎已损坏,您应该在那里有 script_location,因此如果您的迁移位于 alembic 子目录中,alembic。 ini 应该是这样的:

[alembic]
script_location = alembic

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

目录布局必须这样,如果您的script_location = alembic 意味着您有:

alembic.ini
alembic/   # this must match the script_location in alembic.ini
    env.py   # and this must be found at the script_location
    script.py.mako
    versions/
        20c921506336_.py

关于python - 失败 : No config file 'alembic.ini' found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28392347/

相关文章:

python - 蒸馏器。 env.py 中的 ModuleNotFoundError

python - Flask-SQLAlchemy 使用 SQLite 数据库,即使它是为 PostgreSQL 配置的

python - 如何在 sqlalchemy for mysql 中的另一列之后添加一列?

python - 在 Python 中获取数组作为 GET 查询参数

python - 将递归寻峰转换为迭代

python - 存储 NumPy 行和列标题

Python-如何在每次循环后创建新的 Json 文件?

postgresql - 如何使用 SQLAlchemy 使用配置创建 Postgres GIN 索引?

python - 使用默认分隔符与用户定义的分隔符拆分的字符串

python - 删除迁移文件夹后重置 Alembic