python – 运行迁移时出错:sqlalchemy.exc.CompileError:Postgresql ENUM类型需要一个名称

前端之家收集整理的这篇文章主要介绍了python – 运行迁移时出错:sqlalchemy.exc.CompileError:Postgresql ENUM类型需要一个名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我第一次尝试运行升级时,我遇到了这个错误.它似乎在gender_types枚举上失败了,即使我给它一个名字属性.

有没有解决这个问题?有没有办法看到sql生成了什么?

这是我的models.py,这是我运行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",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>",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,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.

解决方法

你需要在枚举中包含name属性,比如db.Enum(‘M’,’F’,name =’gender_types’)

猜你在找的Python相关文章