问题 Alembic - sqlalchemy不检测现有表


我问过一个问题(Alembic - sqlalchemy初始迁移关于如何使用检测表

target_metadata = Base.metadata

对于

alembic revision --autogenerate -m "initial migration"

在我将模型导入env.py文件之后,它似乎工作正常但它没有检测到实际存在的表,因此它创建了包含所有表的迁移文件,例如:

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('Brand',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('slug', sa.String(), nullable=True),
    sa.Column('date_created', sa.DateTime(), nullable=True),
    sa.Column('date_updated', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='Products'
    )

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('ProductFile', schema='Products')

我试过了:

alembic stamp head

但在运行并运行autogenerate命令后,系统再次生成所有模型。

from project.apps.users.models import *
from project.apps.orders.models import *
from project.apps.products.models import *

from project.base import Base, metadata

# 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 = Base.metadata

我该如何避免这个问题?

编辑:

ENV.py:

https://gist.github.com/pypetey/bb65807ce773d8baeaf1

我删除了数据库并进行了迁移

(env) D:\projekty\test>alembic revision --autogenerate
INFO  [alembic.migration] Context impl MSSQLImpl.
INFO  [alembic.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table u'Users.Country'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Brand'
INFO  [alembic.autogenerate.compare] Detected added table u'Users.User'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Product'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.ProductFile
'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.Order'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Category'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Review'
INFO  [alembic.autogenerate.compare] Detected added table u'Users.UserAddress'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.OrderItem'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.OrderStatus'
Generating D:\projekty\test\alembic\versions\1c6337c144a7_.py ... done

(env) D:\projekty\test>alembic upgrade head
INFO  [alembic.migration] Context impl MSSQLImpl.
INFO  [alembic.migration] Will assume transactional DDL.
INFO  [alembic.migration] Running upgrade None -> 1c6337c144a7, empty message

(env) D:\projekty\test>alembic revision --autogenerate
INFO  [alembic.migration] Context impl MSSQLImpl.
INFO  [alembic.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table u'Users.Country'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Brand'
INFO  [alembic.autogenerate.compare] Detected added table u'Users.User'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Product'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.ProductFile
'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.Order'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Category'
INFO  [alembic.autogenerate.compare] Detected added table u'Products.Review'
INFO  [alembic.autogenerate.compare] Detected added table u'Users.UserAddress'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.OrderItem'
INFO  [alembic.autogenerate.compare] Detected added table u'Orders.OrderStatus'
Generating D:\projekty\test\alembic\versions\5abb204549f_.py ... done

7271
2017-10-09 09:34


起源

是什么 alembic current 返回? - dgilland
您是否尝试过首先在空数据库上运行迁移?说跑步 alembic revision --autogenerate ...,如有必要,编辑迁移文件,运行 alembic upgrade head,并运行 alembic revision --autogenerate ... 再次确认生成空迁移文件? - dgilland
@dgilland我已经创建了db,运行了迁移并再次运行自动生成。它没有帮助。查看我更新的帖子。 - Efrin
运行之后 alembic upgrade head什么呢 alembic current 返回?此外,在运行升级之后,您是否可以检查数据库并确认是否已创建表,无论是否 alembic_version 表存在,以及内容是什么 alembic_version 是?根据您的描述,似乎没有创建任何表。您是否有权在数据库上创建表? - dgilland
它们是从头开始创建的。但即使在另一次自动生成后,它确实尝试再次创建它们。我有所有权限。 Alembic当前返回应用了迁移(我这里没有确切的控制台日志)但它确实是但仍然试图再次创建表 - Efrin


答案:


我有同样的问题 - 我不知道它是否仍会影响你。对我来说,问题是由于我使用的架构不是默认的 - 我认为同样的事情正在发生在你身上,因为你正在使用“产品”架构。我发布了一个问题:

https://bitbucket.org/zzzeek/alembic/issue/281/autogenerate-fails-to-detect-existing

并通过一些指导,设法通过设置解决问题 include_schemas=True 同时 run_migrations_* alembic / env.py模块中的函数。

文档

如果为True,则autogenerate将扫描位于的所有模式   SQLAlchemy的 get_schema_names() 方法,并包括所有差异   在所有这些模式中找到的表。使用此选项时,您可以   我也想用 EnvironmentContext.configure.include_object   用于指定可以过滤表/模式的callable的选项   包括在内。


10
2018-03-13 20:23



这是一个但是相关的:能够控制哪些db表被认为是“现有的”: alembic.zzzcomputing.com/en/latest/api/...  和一些旧的讨论: groups.google.com/forum/#!topic/sqlalchemy-alembic/2HJ9J6PiQsk - TaiwanGrapefruitTea
谢谢,伙计,你的老答案仍然拯救了人们的生命! - denied