2014-10-09 50 views
10

我问了一个问题(Alembic - sqlalchemy initial migration) env.py文件它似乎工作正常,但它没有检测到实际存在的表,因此它会创建包含所有表的迁移文件,例如:蒸馏器 - SQLAlchemy的不我已经导入我的模型后,使用</p> <pre><code>target_metadata = Base.metadata </code></pre> <p>为</p> <pre><code>alembic revision --autogenerate -m "initial migration" </code></pre> <p>检测如何检测表中的现有表

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 

,但运行和运行自动生成命令后,系统再次产生的所有车型。

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 

我放弃了DB跑迁移

(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 
+0

'alembic current'返回什么? – dgilland 2014-10-09 14:54:07

+0

您是否尝试过首先在空数据库上运行迁移?通过运行'alembic revision --autogenerate ...',编辑迁移文件(如有必要),运行'alembic upgrade head',并再次运行'alembic revision --autogenerate ...'以确认生成空的迁移文件? – dgilland 2014-10-09 14:56:34

+0

@dgilland我crecreated分贝,跑了迁移,并再次运行自动生成。它没有帮助。检查我更新的帖子。 – Efrin 2014-10-10 10:38:29

回答

8

我有此相同的问题 - 我不知道,如果它仍然影响你。对我来说,这个问题是由于我使用的模式不是默认的 - 我认为同样的事情发生在你身上,因为你正在使用“产品”模式。我张贴的问题在:

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

并指导了一点点,设法通过设置在蒸馏器/ env.py模块中都run_migrations_*功能include_schemas=True来解决问题。

docs

如果真,将自动生成跨()位于由 SQLAlchemy的get_schema_names所有模式法扫描,并且包括在所有这些模式中 表中的所有分歧。当使用此选项时,您可能还想使用EnvironmentContext.configure.include_object 选项来指定一个可调用函数,该函数可以过滤包含 的表/模式。

+0

这是一个旁边,但相关:能够控制什么数据库表被视为“现有”:http://alembic.zzzcomputing.com/en/latest/api/runtime .html#alembic.runtime.environment.EnvironmentContext.configure.params.include_object以及一些关于此的较早讨论:https://groups.google.com/forum/#!topic/sqlalchemy-alembic/2HJ9J6PiQsk – TaiwanGrapefruitTea 2016-12-17 16:04:31

相关问题