失败:没有配置文件' alembic.ini'发现

时间:2015-02-08 09:11:03

标签: python alembic

我试图改变Alembic但是当我尝试运行Alembic时我得到了错误。我是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 ###

2 个答案:

答案 0 :(得分:7)

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

似乎你的alembic ini坏了,你应该有script_location,因此如果你的迁移在alembic子目录中,alembic.ini应该是这样的:< / p>

[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

答案 1 :(得分:6)

对于执行此类操作的部署脚本:

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未能在script_location中解释您对alembic.ini的意图。通常您使用alembic.ini从目录运行alembic,而alembic能够将您的script_location解释为相对路径。

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

如果查看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升级,而是将bash脚本发送到cd到正确的目录,然后运行它。

相关问题