我试图Alembic第一次使用,并希望使用此处--autogenerate描述的功能
我的项目结构看起来像
project/
configuration/
__init__.py
dev.py
test.py
core/
app/
models/
__init__.py
user.py
db/
alembic/
versions/
env.py
alembic.ini
Run Code Online (Sandbox Code Playgroud)
我使用Flask,并SQLAlchemy和他们的Flask-SQLAlchemy扩展.我的模特User看起来像
class User(UserMixin, db.Model):
__tablename__ = 'users'
# noinspection PyShadowingBuiltins
uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
unique=True)
email = Column('email', String, nullable=False, unique=True)
_password = Column('password', String, nullable=False)
created_on = Column('created_on', sa.types.DateTime(timezone=True),
default=datetime.utcnow())
last_login = Column('last_login', sa.types.DateTime(timezone=True),
onupdate=datetime.utcnow())
Run Code Online (Sandbox Code Playgroud)
如上所述这里,我修改env.py的样子
from configuration import app
alembic_config = config.get_section(config.config_ini_section) …Run Code Online (Sandbox Code Playgroud) 我试图首次对预先存在的数据库自动生成一个alembic修订版,但是当我运行以下命令时
alembic revision --autogenerate
Run Code Online (Sandbox Code Playgroud)
它会生成一个迁移,尝试在我的数据库中创建每个表和索引.与此类似:
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('table1',
sa.Column('id', sa.SmallInteger(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=True),
sa.Column('desc', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name'),
schema='schema1'
)
op.create_index(op.f('ix_index1'), 'table1', ['name'], unique=False, schema='schema1')
... all my other tables/indexes ..
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_index1'), table_name='table1', schema='schema1')
op.drop_table('table1', schema='schema1')
... all my other tables/indexes ..
Run Code Online (Sandbox Code Playgroud)
然后,如果我尝试运行迁移,它将失败,因为对象已经存在:
sqlalchemy.exc.ProgrammingError: (ProgrammingError) relation "table1" already exists
Run Code Online (Sandbox Code Playgroud)
所以在我看来,像alembic认为我的数据库不包含任何表,但确实如此.
任何想法为什么会这样?
我在创建初始迁移时遇到问题,该迁移会自动拥有我在models.py中使用共享Base(declarative_base)定义的表.
当我输入命令时:
alembic revision --autogenerate
Run Code Online (Sandbox Code Playgroud)
alembic创建一个空文件.
我的配置或方法有什么问题?
project.base.py:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
Run Code Online (Sandbox Code Playgroud)
env.py:
import sys
import os
sys.path.append(os.path.abspath(os.getcwd()))
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
from project.base import Base
target_metadata = Base.metadata
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)
# target_metadata.reflect(engine, …Run Code Online (Sandbox Code Playgroud)