Alembic - sqlalchemy不检测现有表

时间:2014-10-09 09:34:55

标签: python sqlalchemy alembic

我已经问了一个关于如何使用

检测表格的问题(Alembic - sqlalchemy initial migration
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

1 个答案:

答案 0 :(得分:17)

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

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

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

请参阅docs

  

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