flask-migrate stuck on downgrade with table containing Enum field

524 Views Asked by At

I have defined the following models using sqlalchemy and my database backend is mysql.

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
    
class Vehicle(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)

    def __str__(self):
    return self.name


class Branch(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, nullable=False)
    tracking_radius = db.Column(db.Integer)
    default_basket_size = db.Column(db.Float)
    product = db.Column(
    db.Enum(
        "x",
        "y",
        "Default",
        name="product_enum"),
        nullable=False)
    ignore_timeslot = db.Column(db.Boolean, nullable=False)

    def __str__(self):
    return self.name


class PricingConfig(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, nullable=False)
    base_fare = db.Column(db.Integer, nullable=False)
    base_mileage = db.Column(db.Integer, nullable=False)
    mileage_fare = db.Column(db.Integer, nullable=False)
    terminal_fare = db.Column(db.Integer, nullable=False)

    def __str__(self):
    return self.name


class TemporalConfig(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, nullable=False)
    create_to_accept = db.Column(db.Integer, nullable=False)
    accept_to_arrive = db.Column(db.Integer, nullable=False)
    arrive_to_pickup = db.Column(db.Integer, nullable=False)
    pickup_per_order = db.Column(db.Integer, nullable=False)
    arrive_to_deliver = db.Column(db.Integer, nullable=False)
    max_trip_duration = db.Column(db.Integer, nullable=False)

    def __str__(self):
    return self.name


    

I have used flask-migrate for creating the tables. The migration file generated by alembic is as follows:

"""empty message

Revision ID: 0c88c06df5ad
Revises: 
Create Date: 2020-11-10 19:42:34.096684

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0c88c06df5ad'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('branch',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('tracking_radius', sa.Integer(), nullable=True),
    sa.Column('default_basket_size', sa.Float(), nullable=True),
    sa.Column('product', sa.Enum('x', 'y', 'Default', name='product_enum'), nullable=False),
    sa.Column('ignore_timeslot', sa.Boolean(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('pricing_config',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('base_fare', sa.Integer(), nullable=False),
    sa.Column('base_mileage', sa.Integer(), nullable=False),
    sa.Column('mileage_fare', sa.Integer(), nullable=False),
    sa.Column('terminal_fare', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('temporal_config',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('create_to_accept', sa.Integer(), nullable=False),
    sa.Column('accept_to_arrive', sa.Integer(), nullable=False),
    sa.Column('arrive_to_pickup', sa.Integer(), nullable=False),
    sa.Column('pickup_per_order', sa.Integer(), nullable=False),
    sa.Column('arrive_to_deliver', sa.Integer(), nullable=False),
    sa.Column('max_trip_duration', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('vehicle',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('vehicle')
    op.drop_table('temporal_config')
    op.drop_table('pricing_config')
    op.drop_table('branch')
    # ### end Alembic commands ###
    
    

The flask db upgrade and flask db downgrade commands work fine if i discard the branch table entirely, but downgrade becomes stuck indefinitely when attempting to drop the branch table. I have checked for any potential locks on the tables using the show open tables command in mysql terminal but there are no locks on any tables. I reckon this problem is related to the Enum column defined in the product field but i still don't understand the source of this problem and how to fix it.

0

There are 0 best solutions below