蒸馏 - 自发产生空迁移 [英] Alembic --autogenerate producing empty migration

查看:139
本文介绍了蒸馏 - 自发产生空迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用 Alembic 并且想要使用 - autogenerate 描述 here

我的项目结构看起来像是:

$ p $ $ $ $ $
$ b $ _ $ __init__.py
dev.py
test.py
core /
app /
models /
__init__.py
user.py
db /
alembic /
版本/
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())

正如这里,我修改了 env.py 看起来像

 来自配置imp ort 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)

 从配置导入db 


target_metadata = db。元数据

其中配置.__ init__py / p>

  from flask import Flask 
from flask.ext.sqlalchemy import SQLAlchemy
import dev


app = Flask(__ name__)
app.config.from_envvar('SETTINGS_PT')
db = SQLAlchemy(app)
pre

现在当我运行迁移

$ $ $ code $ $ alembic revision - autogenerate -m添加用户表
INFO [alembic.migration]上下文impl PostgresqlImpl。
INFO [alembic.migration]将承担事务性的DDL。
Generating / Users / me / IdeaProjects / project / db / alembic / versions / 55a9d5
35d8ae_added_user_table.py ... done


$ b

但文件 alembic / versions / 55a9d5 有空 upgrade() downgrade()方法

 新增用户表

版本号:1b62a62eef0d
修正:无
创建日期:2013-03-27 06:37:08.314177


$ b由Alembic使用的$ b#修订标识符。
revision ='1b62a62eef0d'
down_revision = None

from alembic import op
import sqlalchemy as sa


def升级():
###由Alembic自动生成的命令 - 请调整! ###
传递
###结束Alembic命令###


def downgrade():
###由Alembic自动生成的命令 - 请调整! ###
传递
###结束Alembic命令###

为什么不能理解有一个新的 User 模型?
请帮助

解决方案

按照@zzzeek,在我的 env中包含以下内容.py ,我可以使用 - autogenerate 选项



code> env.py run_migrations_online()

 从配置导入应用程序
从core.expense.models导入用户#在此处添加我的模型

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 )

然后我运行 alembic revision --autogenerate -m新增了初始表得到了

  def upgrade():
###命令auto generated by Alembic - 请调整! ###
op.create_table('users',
sa.Column('uuid',sa.GUID(),nullable = False),
sa.Column('email', sa.String(),nullable = False),
sa.Column('password',sa.String(),nullable = False),
sa.Column('created_on',sa.DateTime timezone = True),nullable = True),
sa.Column('last_login',sa.DateTime(timezone = True),nullable = True),
sa.PrimaryKeyConstraint('uuid'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('uuid')

###结束Alembic命令###
$ b $

谢谢迈克尔的帮助!


I am trying to use Alembic for the first time and want to use --autogenerate feature described here

My project structure looks like

project/
       configuration/
                    __init__.py
                    dev.py
                    test.py
       core/
           app/
              models/
                    __init__.py
                    user.py
       db/
          alembic/
                  versions/
                  env.py
          alembic.ini

I am using Flask and SQLAlchemy and their Flask-SQLAlchemy extension. my model User looks like

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())

As described here, I modified env.py to look like

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)

and

from configuration import db


target_metadata = db.metadata

where configuration.__init__py looks like

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import dev


app = Flask(__name__)
app.config.from_envvar('SETTINGS_PT')
db = SQLAlchemy(app)

Now when I run migration

$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

but file alembic/versions/55a9d5 has empty upgrade() and downgrade() methods

"""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 ###

How come it is not able to understand that there is a new User model? Please help

解决方案

As per @zzzeek, after I included the following in my env.py, I was able to work with --autogenerate option

in env.py under run_migrations_online()

from configuration import app
from core.expense.models import user # added my model here

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)

then I ran alembic revision --autogenerate -m "Added initial table" and got

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('users',
    sa.Column('uuid', sa.GUID(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('password', sa.String(), nullable=False),
    sa.Column('created_on', sa.DateTime(timezone=True), nullable=True),
    sa.Column('last_login', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('uuid')
    )
    ### end Alembic commands ###

Thank you Michael for all your help!

这篇关于蒸馏 - 自发产生空迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆