25

我问了一个关于如何使用检测表的问题( 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
4

2 回答 2

39

我有这个完全相同的问题 - 我不知道它是否仍然影响你。对我来说,问题是因为我使用的架构不是默认的——我认为同样的事情发生在你身上,因为你使用的是“产品”架构。我在以下位置发布了一个问题:

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

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

请参阅文档

如果为 True,自动生成将扫描由 SQLAlchemy get_schema_names()方法定位的所有模式,并包括在所有这些模式中找到的表中的所有差异。使用此选项时,您可能还希望使用EnvironmentContext.configure.include_object 选项来指定一个可调用的对象,该可调用对象可以过滤包含的表/模式。

于 2015-03-13T20:23:48.570 回答
0

我有一个观察。
导入到env.pyalembic 中的元数据,必须具有所有模型元数据。所以必须在调用 Base 之前加载所有模型。所以,
db_setup.py会有,

...
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
...

并且models.py会有,

from .db_setup import Base
from sqlalchemy import Column, String
class TestModel(Base):
    __tablename__ = "test"
    field_1 = Column(String)
    ...

现在,在main.py,

from flask import Flask
...
from models.setup import Base
app = Flask(__name__)
...

最后,在蒸馏器env.py中,

...
...
from main import Base # Not from db_setup!
target_metadata = Base.metadata
...
...
于 2022-01-28T07:44:39.007 回答