运行迁移时出错:sqlalchemy.exc.CompileError:Postgresql ENUM类型需要一个名称 [英] Error when running migrations: sqlalchemy.exc.CompileError: Postgresql ENUM type requires a name

查看:243
本文介绍了运行迁移时出错:sqlalchemy.exc.CompileError:Postgresql ENUM类型需要一个名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试首次运行升级时遇到此错误。即使我已经给它提供了名称属性,它似乎也无法通过sex_types枚举。

I'm running into this error when I attempt to run upgrade for the first time. It seems to be failing on the gender_types enum, even though I have given it a name attribute.

是否可以解决此问题?有什么方法可以查看生成的SQL吗?

Is there a fix for this? Is there some way to see what SQL is getting generated?

这是我的models.py,这是我在运行flask.ext.migrate.upgrade时遇到的错误。

This is my models.py and this is the error I get when I run flask.ext.migrate.upgrade.

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    first = db.Column(db.String(64))
    last = db.Column(db.String(64))
    dob = db.Column(db.Date)
    street_addr1 = db.Column(db.String(64))
    street_addr2 = db.Column(db.String(64))
    city = db.Column(db.String(64))
    state = db.Column(db.String(2))
    zip = db.Column(db.String(9))
    gender = db.Column(db.Enum('M', 'F', name='gender_types'))
    home_box = db.Column(db.String(32))
    username = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)
    def __repr__(self):
        return '<User %r>' % self.username

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class Competition(db.Model):
    __tablename__ = 'competitions'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(12))

终端输出:

(venv)Nates-MacBook-Pro-2:competitions nate$ heroku run python manage.py deploy
Running `python manage.py deploy` attached to terminal... up, run.8483
INFO  [alembic.migration] Context impl PostgresqlImpl.
INFO  [alembic.migration] Will assume transactional DDL.
INFO  [alembic.migration] Running upgrade None -> 3c7e6046cf6, initial migration
Traceback (most recent call last):
  File "manage.py", line 36, in <module>
    manager.run()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/__init__.py", line 405, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/__init__.py", line 384, in handle
    return handle(app, *positional_args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_script/commands.py", line 145, in handle
    return self.run(*args, **kwargs)
  File "manage.py", line 25, in deploy
    upgrade()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask_migrate/__init__.py", line 98, in upgrade
    command.upgrade(config, revision, sql = sql, tag = tag)
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/command.py", line 124, in upgrade
    script.run_env()
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/script.py", line 199, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/util.py", line 199, in load_python_file
    module = load_module(module_id, path)
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/compat.py", line 55, in load_module
    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 "/app/.heroku/python/lib/python2.7/site-packages/alembic/environment.py", line 652, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/migration.py", line 225, in run_migrations
    change(**kw)
  File "migrations/versions/3c7e6046cf6_initial_migration.py", line 33, in upgrade
    sa.PrimaryKeyConstraint('id')
  File "<string>", line 7, in create_table
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/operations.py", line 647, in create_table
    self._table(name, *columns, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/alembic/ddl/impl.py", line 148, in create_table
    _ddl_runner=self)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/event.py", line 409, in __call__
    fn(*args, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 404, in __call__
    return getattr(self.target, self.name)(*arg, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/types.py", line 1933, in _on_table_create
    t._on_table_create(target, bind, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/base.py", line 870, in _on_table_create
    self.create(bind=bind, checkfirst=checkfirst)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/base.py", line 821, in create
    bind.execute(CreateEnumType(self))
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 662, in execute
    params)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 714, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/expression.py", line 1920, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2954, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/interfaces.py", line 804, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/interfaces.py", line 823, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 80, in _compiler_dispatch
    return meth(self, **kw)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1072, in visit_create_enum_type
    self.preparer.format_type(type_),
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1254, in format_type
    raise exc.CompileError("Postgresql ENUM type requires a name.")
sqlalchemy.exc.CompileError: Postgresql ENUM type requires a name.


推荐答案

您需要在Enum中包括name属性,例如 db.Enum('M','F',name ='gender_types')

You need to include name attribute in the Enum, like db.Enum('M', 'F', name='gender_types')

这篇关于运行迁移时出错:sqlalchemy.exc.CompileError:Postgresql ENUM类型需要一个名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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