Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when running migrations: sqlalchemy.exc.CompileError: Postgresql ENUM type requires a name

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.

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

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))

Terminal output:

(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.
like image 490
Nate Reed Avatar asked Jun 17 '14 02:06

Nate Reed


1 Answers

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

like image 65
ambi Avatar answered Oct 22 '22 10:10

ambi