我问了一个关于如何使用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我删除了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发布于 2015-03-14 04:23:49
我也有同样的问题--我不知道它是否仍然影响着你。对我来说,这个问题是因为我使用的模式不是默认的-我认为同样的事情也发生在您身上,因为您使用的是"Products“模式。我在以下位置发布了一期:
https://bitbucket.org/zzzeek/alembic/issue/281/autogenerate-fails-to-detect-existing
在一些指导下,通过在alembic/env.py模块的两个run_migrations_*函数中设置include_schemas=True,设法解决了这个问题。
请参阅docs
如果为True,则autogenerate将扫描由SQLAlchemy get_schema_names()方法定位的所有架构,并包括在所有这些架构中找到的表中的所有差异。在使用此选项时,您可能还希望使用EnvironmentContext.configure.include_object选项来指定一个可调用对象,该可调用对象可以过滤所包含的表/模式。
发布于 2022-01-28 07:44:39
我有一个观察。
在alembic中导入到env.py的元数据必须具有所有模型元数据。因此,在调用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__)
...最后,在alembic env.py中,
...
...
from main import Base # Not from db_setup!
target_metadata = Base.metadata
...
...https://stackoverflow.com/questions/26275041
复制相似问题