2014-06-17 42 views
5

我在第一次嘗試運行升級時遇到此錯誤。它似乎在gender_types枚舉上失敗了,儘管我給了它一個名稱屬性。運行遷移時出錯:sqlalchemy.exc.CompileError:Postgresql ENUM類型需要名稱

有沒有解決這個問題?有什麼方法可以查看生成的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", 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. 
+1

你看不到的SQL,因爲這是一個SQLAlchemy的編譯錯誤,這是因爲它甚至不知道如何發出SQL。這聽起來像alembic遷移不包括'Enum'規範中的名稱;你可以檢查'migrations/versions/3c7e6046cf6_initial_migration.py'是否屬於這種情況嗎? – univerio

+1

我想到了遷移是舊的,並且基於在Enum中沒有包含name屬性的模型。謝謝,我重新創建了遷移,現在工作正常。 –

+0

您應該添加一個答案作爲答案,因爲知道枚舉需要名稱是有用的,並且您應該在運行它們之前檢查生成的遷移。 – davidism

回答

9

您需要在枚舉name屬性,就像db.Enum('M', 'F', name='gender_types')

相關問題