【问题标题】:FAILED: No config file 'alembic.ini' found失败:找不到配置文件“alembic.ini”
【发布时间】:2015-04-08 03:56:18
【问题描述】:

我尝试在 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 ###

【问题讨论】:

  • 这个问题解决了吗?这些答案都不适合我。

标签: python alembic


【解决方案1】:

必须cdalembic.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

【讨论】:

  • 我做到了,但得到了这个 (venv)peg@peg:~/flask-Alembic/migrations$ alembic current 找不到记录器“alembic.util”的处理程序失败:没有“script_location”键在配置中找到。 @Antti Haapala
  • 我的也一样,所以我有完全相同的@Antti Haapala
【解决方案2】:

对于执行如下操作的部署脚本:

ssh ... alembic upgrade head

Antii 的答案提供了直接答案,即 -c 选项:

ssh ... alembic -c /path/to/alembic.ini upgrade head

但是你可能会像我一样:

FAILED: Path doesn't exist: 'alembic'. Please use the 'init' command to create a new scripts folder.

Alembic 未能在 alembic.ini 中解释您对 script_location 的意图。通常,您从带有 alembic.ini 的目录运行 alembic,并且 alembic 能够将您的 script_location 解释为相对路径。

但是,当您使用简单的部署脚本或以其他方式从不同目录运行它时,它不知道在哪里查找,并且它不使用 alembic.ini 的目录来猜测位置(我认为 Antii 建议? )。

如果您查看 alembic 源代码 here,您可以看到 alembic 需要 1) 绝对路径、2) 包路径或 3) 工作目录的相对路径(默认情况)。

因此这个答案的第二级是

选项 1:绝对路径

(我没有测试过这个!)在alembic.ini 中提供alembic 目录的绝对路径。这对我不起作用,因为代码变得不可移植:

...
script_location = /path/to/alembic/
...

选项 2:包路径

alembic.ini中提供包路径:

...
script_location = rootpackage:alembic  # or maybe rootpacakge.xyz:alembic
...

当然它实际上需要是一个包,所以带有alembic.ini 的位置必须有__init__.py

选项 3:相对路径

不要直接在部署脚本中运行 alembic upgrade ,而是将 bash 脚本指向 cd 到正确的目录,然后运行它。

【讨论】:

  • 很好的答案。对于相对路径问题,请使用 docker 或其他虚拟环境解决方案以实现跨机可移植性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2015-09-27
  • 1970-01-01
  • 2021-11-09
相关资源
最近更新 更多