AttributeError:' int'对象没有属性' _compiler_dispatch'

时间:2014-06-12 17:37:02

标签: flask sqlalchemy alembic

我正在使用flask-sqlalchemy扩展程序与alembic进行迁移。当我尝试添加新的迁移文件并将架构升级到最新版本时,我收到以下错误:

AttributeError: 'int' object has no attribute '_compiler_dispatch'

迁移文件的内容:

revision = 'ec2c2d40eb1'
down_revision = '28dda873b826'

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.alter_column(
        'users',
        'wiki_permission',
        new_column_name='wiki_group',
        nullable=False,
        existing_nullable=False,
        type_=sa.Integer(),
        existing_type=sa.Integer(),
        server_default=1,
        existing_server_default=1          # Line of error - 27
    )   


def downgrade():
    op.alter_column(
        'users',
        'wiki_group',
        new_column_name='wiki_permission',
        nullable=False,
        existing_nullable=False,
        type_=sa.Integer(),
        existing_type=sa.Integer(),
        server_default=1,
        existing_server_default=1
    )

感谢您花时间帮助我。

修改

完整的错误消息:

INFO  [alembic.migration] Context impl MySQLImpl.
INFO  [alembic.migration] Will assume non-transactional DDL.
INFO  [alembic.migration] Running upgrade 28dda873b826 -> ec2c2d40eb1, users change column wiki_permission to wiki_group
Traceback (most recent call last):
  File "/home/kevin/Code/python/flask/terminus/venv/bin/alembic", line 9, in <module>
    load_entry_point('alembic==0.6.5', 'console_scripts', 'alembic')()
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/config.py", line 298, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/config.py", line 293, in main
    self.run_cmd(cfg, options)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/config.py", line 279, in run_cmd
    **dict((k, getattr(options, k)) for k in kwarg)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/command.py", line 125, in upgrade
    script.run_env()
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/script.py", line 203, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/util.py", line 212, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/compat.py", line 58, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "alembic/env.py", line 77, in <module>
    run_migrations_online()
  File "alembic/env.py", line 70, in run_migrations_online
    context.run_migrations()
  File "<string>", line 7, in run_migrations
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/environment.py", line 688, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/migration.py", line 258, in run_migrations
    change(**kw)
  File "alembic/versions/ec2c2d40eb1_users_change_column_wiki_permission_to_.py", line 27, in upgrade
    existing_server_default=1,
  File "<string>", line 7, in alter_column
  File "<string>", line 1, in <lambda>
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/util.py", line 329, in go
    return fn(*arg, **kw)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/operations.py", line 317, in alter_column
    existing_autoincrement=existing_autoincrement
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/ddl/mysql.py", line 44, in alter_column
    else existing_autoincrement
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 76, in _exec
    conn.execute(construct, *multiparams, **params)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 720, in execute
    return meth(self, multiparams, params)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 67, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 768, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File "<string>", line 1, in <lambda>
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 468, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 25, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 197, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 220, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py", line 410, in <lambda>
    lambda *arg, **kw: existing(*arg, **kw))
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/ext/compiler.py", line 448, in __call__
    return fn(element, compiler, **kw)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/ddl/mysql.py", line 171, in _mysql_change_column
    autoincrement=element.autoincrement
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/ddl/mysql.py", line 190, in _mysql_colspec
    spec += " DEFAULT %s" % _render_value(compiler, server_default)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/alembic/ddl/mysql.py", line 179, in _render_value
    return compiler.sql_compiler.process(expr)
  File "/home/kevin/Code/python/flask/terminus/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/compiler.py", line 220, in process
    return obj._compiler_dispatch(self, **kwargs)
AttributeError: 'int' object has no attribute '_compiler_dispatch'

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚经历了同样的问题。我不是使用flask-sqlalchemy,只是直接的alembic,但它应该是相同的。

  1. 其次,sa.Integer没有括号,对我有用,所以我建议这样做。

  2. alembic docs说:

      

    在生成与MySQL兼容的迁移文件时,建议now_type,existing_server_default和existing_nullable参数存在,如果不被更改

    由于您似乎更改了这些列,因此文档建议它们应该存在。所以:

    • 移除nullabletype_server_default。它们没有被改变。
    • 保留existing_nullableexisting_typeexisting_server_default