我试图使用Alembic
首次并希望使用--autogenerate
功能描述here蒸馏器--autogenerate生产空迁移
我的项目结构看起来像
project/
configuration/
__init__.py
dev.py
test.py
core/
app/
models/
__init__.py
user.py
db/
alembic/
versions/
env.py
alembic.ini
我使用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())
如上所述here,我修改env.py
看起来像
from configuration import app
alembic_config = config.get_section(config.config_ini_section)
alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
engine = engine_from_config(
alembic_config,
prefix='sqlalchemy.',
poolclass=pool.NullPool)
和
from configuration import db
target_metadata = db.metadata
其中configuration.__init__py
看起来像
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import dev
app = Flask(__name__)
app.config.from_envvar('SETTINGS_PT')
db = SQLAlchemy(app)
现在,当我运行迁移
$alembic revision --autogenerate -m "Added user table"
INFO [alembic.migration] Context impl PostgresqlImpl.
INFO [alembic.migration] Will assume transactional DDL.
Generating /Users/me/IdeaProjects/project/db/alembic/versions/55a9d5
35d8ae_added_user_table.py...done
但文件alembic/versions/55a9d5
具有空upgrade()
和downgrade()
方法
"""Added user table
Revision ID: 1b62a62eef0d
Revises: None
Create Date: 2013-03-27 06:37:08.314177
"""
# revision identifiers, used by Alembic.
revision = '1b62a62eef0d'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
为什么它不能够理解,有一个新的User
模式? 请帮助
您的蒸馏器env.py必须确保它已导入的用户模式,使表存在于内存时迁移运行。 – zzzeek 2013-03-27 19:35:33
就是这样!非常感谢您的帮助Michael – daydreamer 2013-03-27 19:44:53
可能有用:如果在更新列后生成空迁移,请选中此答案http://stackoverflow.com/a/17176843/1339254 – alexche8 2017-01-31 16:02:32