在使用alembic进行迁移时,“ALTER”语法错误,用于flask_sqlalchemy

时间:2017-06-19 14:25:50

标签: python flask sqlalchemy alembic

当我向现有表添加新列(nullable = False)时,我需要手动更新迁移修订文件,首先使用nullable = True添加列,然后更新所有现有记录以设置列之后将列更改为nullable = False。但我遇到了“ALTER”错误:语法错误。

这是测试脚本(test.py):

#!/usr/bin/env python
import os

import flask_migrate
from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

manager = Manager(app)
db = SQLAlchemy(app)

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)

migrate = flask_migrate.Migrate(app, db)
manager.add_command('db', flask_migrate.MigrateCommand)

if __name__ == '__main__':
    manager.run()

首先,我初始化数据库并将其升级到最新版本:

$ python test.py db init && python test.py db migrate && python test.py db upgrade
  Creating directory /tmp/test/migrations ... done
  Creating directory /tmp/test/migrations/versions ... done
  Generating /tmp/test/migrations/env.pyc ... done
  Generating /tmp/test/migrations/script.py.mako ... done
  Generating /tmp/test/migrations/env.py ... done
  Generating /tmp/test/migrations/alembic.ini ... done
  Generating /tmp/test/migrations/README ... done
  Please edit configuration/connection/logging settings in '/tmp/test/migrations/alembic.ini' before proceeding.
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'users'
  Generating /tmp/test/migrations/versions/86805d015930_.py ... done
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 86805d015930, empty message

然后我更新了模型以添加新列'email',它是nullable = False:

<snip>
...
class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)  # this is the new column

然后生成迁移修订文件:

$ python test.py db migrate -m "add name"
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column 'users.name'
  Generating /tmp/test/migrations/versions/c89371227a53_add_name.py ... done

由于name列不可为空,因此需要手动更新迁移文件,如下所示进行更新:

$ cat /tmp/test/migrations/versions/c89371227a53_add_name.py

from alembic import op
import sqlalchemy as sa

revision = 'c89371227a53'
down_revision = '45a51b6df68c'
branch_labels = None
depends_on = None

def upgrade():
    op.add_column('users', sa.Column('name', sa.String(length=64), nullable=True))
    op.execute("""
    UPDATE users SET name="foobar"
    """)
    op.alter_column('users', 'name', nullable=False)


def downgrade():
    op.drop_column('users', 'name')

现在运行迁移:

$ python test.py db upgrade

我收到如下错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "ALTER": syntax error [SQL: u'ALTER TABLE users ALTER COLUMN name SET NOT NULL']

如何解决此问题或如何针对此类情况进行迁移?

我的环境是:

Flask==0.12.1
Flask-Migrate==2.0.3
Flask-Script==2.0.5
Flask-SQLAlchemy==2.2
SQLAlchemy==1.1.9
alembic==0.9.1

1 个答案:

答案 0 :(得分:4)

只是想出原因,因为我使用sqlite和sqlite缺乏ALTER支持,解决方法就是使用批处理操作迁移http://alembic.zzzcomputing.com/en/latest/batch.html