2

Using flask-migrate before I was able to create enums by using flask-sqlalchemy's db.Enum and entering the values in as strings like so.

reservation_status = db.Enum('pending', 'confirmed, name='reservation_status_enum')

I decided to start using enum classes like the following. According to the sqlalchemy docs works fine.

class Status(enum.Enum):
    pending = 'pending'
    confirmed = 'confirmed'
    rejected = 'rejected'
    abandoned = 'abandoned'

reservation_status = db.Enum(Status, name='reservation_status_enum')

class Reservation(db.Model):
    __tablename__ = 'reservations'

    id = db.Column(db.Integer, primary_key=True)
    status = db.Column(reservation_status, default=Status.pending)
    ...

When I try to use the migrate command I get an invalid syntax error in the generated code as follows. The error is exactly what was written to the file.

    sa.Column('status', sa.Enum(<enum 'Status'>, name='reservation_status_enum'), nullable=True),
                            ^
SyntaxError: invalid syntax
davidism
  • 121,510
  • 29
  • 395
  • 339
  • which database are you using? – danidee Aug 27 '16 at 13:13
  • According to your link, support for `enum.Enum` was added to SQLAlchemy in version 1.1. The most recent (non pre-release) version of SQLAlchemy is 1.0.14. I wouldn't expect Alembic to be updated until after SQLAlchemy 1.1 is released. – dirn Aug 27 '16 at 13:22
  • You know, I was wrapped up in the code I forgot about versioning. That would make sense. Seems like for now I am stuck with string enums. Thank you @dim. I am using sqlite for testing danidee – Crysthian Chavez Aug 27 '16 at 13:32

1 Answers1

1

Thanks to user @dim, I was able to resolve the issue by upgrading just sqlalchemy to beta 1.1.0b3. As is most cases, it was user error(me).

All i did was

pip uninstall sqlalchemy
pip install sqlalchemy==1.1.0b3