无法使用alter_column重命名列

时间:2015-01-25 21:06:24

标签: alembic flask-migrate

我有一个基于pylons和sqlalchemy的现有项目。我没有在这个项目中使用alembic或任何包装器。

我将项目迁移到flask和sqlalchemy。 我使用了旧版本1.5的旧数据库,并使用Flast-Migrate生成了第一个迁移脚本。嗯,我意识到我必须手动做很多事情。

重命名列时,我已经卡住了。我正在使用SQLite数据库。

旧项目有一个表“Token”,它被重命名为“token”。显然有一些数据库不关心大写或小写。 当我尝试重命名第一列时:

op.alter_column('token', 'privacyIDEATokenId', new_column_name='id')

我收到此错误:

sqlalchemy.exc.OperationalError: (OperationalError) near \
""privacyIDEATokenId"": syntax error u'ALTER TABLE token RENAME \
"privacyIDEATokenId" TO id' ()

列Token.privacyIDEATokenId是新版本中应为token.id的主键。 对于不是主键的列也会发生同样的情况。

完整追踪:

Traceback (most recent call last):
  File "./manage.py", line 107, in <module>
    manager.run()
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/flask_migrate/__init__.py", line 98, in upgrade
    command.upgrade(config, revision, sql = sql, tag = tag)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/command.py", line 129, in upgrade
    script.run_env()
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/script.py", line 208, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/util.py", line 230, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/compat.py", line 63, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 72, in <module>
    run_migrations_online()
  File "migrations/env.py", line 65, in run_migrations_online
    context.run_migrations()
  File "<string>", line 7, in run_migrations
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/environment.py", line 696, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/migration.py", line 266, in run_migrations
    change(**kw)
  File "migrations/versions/112475c7f45_.py", line 103, in upgrade
    _upgrade_token_table()
  File "migrations/versions/112475c7f45_.py", line 49, in _upgrade_token_table
    op.alter_column('token', 'privacyIDEATokenId', new_column_name='id')
  File "<string>", line 7, in alter_column
  File "<string>", line 1, in <lambda>
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/util.py", line 353, in go
    return fn(*arg, **kw)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/operations.py", line 329, in alter_column
    existing_autoincrement=existing_autoincrement
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 131, in alter_column
    existing_nullable=existing_nullable,
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 81, in _exec
    conn.execute(construct, *multiparams, **params)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 729, in execute
    return meth(self, multiparams, params)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 69, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 783, in _execute_ddl
    compiled
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 958, in _execute_context
    context)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception
    exc_info
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 951, in _execute_context
    context)
  File "/home/cornelius/src/privacyidea/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 436, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) near ""privacyIDEATokenId"": syntax error u'ALTER TABLE token RENAME "privacyIDEATokenId" TO id' ()

2 个答案:

答案 0 :(得分:6)

很明显,sqlite无法重命名列。最后,我最终创建了具有新列的新表,并将数据从一个推送到另一个。

这没关系,因为我意识到,在重新写入新表之前,我还必须修改数据,调整和修改数据。

我最终得到的脚本在这里: https://github.com/privacyidea/privacyidea/blob/version2/migrations/versions/4f32a4e1bf33_.py#L241

答案 1 :(得分:3)

现在你可以使用&#34; batch migration&#34;在SQLite下启用ALTER-ing列:

with op.batch_alter_table('table_name') as bop:
    bop.alter_column('old_column_name', new_column_name='new_name')