5

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

我将项目迁移到烧瓶和 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' ()
4

2 回答 2

8

现在您可以使用“批量迁移”来启用 SQLite 下的 ALTER-ing 列:

with op.batch_alter_table('table_name') as bop:
    bop.alter_column('old_column_name', new_column_name='new_name')
于 2018-05-15T22:01:48.827 回答
7

显然 sqlite 不能重命名列。所以最后我最终创建了带有新列的新表并将数据从一个推到另一个。

没关系,因为我意识到,在再次写入新表之前,我还必须对数据进行修改、调整和修改。

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

于 2015-01-26T20:20:05.530 回答