Crysthian Chavez
Crysthian Chavez

Reputation: 43

Flask-Migrate having issue with enum class in models

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

Upvotes: 2

Views: 1179

Answers (1)

Crysthian Chavez
Crysthian Chavez

Reputation: 43

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

Upvotes: 1

Related Questions