使用 sqlalchemy_utils ChoiceType 在 Flask 中迁移和升级时出错

Error in migrating and upgrading in Flask with sqlalchemy_utils ChoiceType

我正在使用 flask-migrate 来调整我的数据库更改。

$python manage.py db init
$python manage.py db migrate

这些工作正常但是当我使用

$python manage.py db upgrade

发生此错误

TypeError: <flask_script.commands.Command object at 0x7fa134712890>: __init__() got an unexpected keyword argument 'length'

这是我的models.py

class JobsModel(db.Model):
JOB_STATUS_TYPES = [
  (u"data_upload_in_progress", u"Data Upload in Progress"),
  (u"data_upload_success", u"Data Upload Success"),
  (u"data_validation_success", u"Data Validation Success"),
  (u"data_validation_error", u"Data Validation Error")
   ]

 __tablename__ = 'jobs'

 job_id = db.Column(db.Integer, primary_key = True)
 usecase_id = db.Column(db.Integer, db.ForeignKey('use_cases.id'), nullable = False)
 usecase = db.relationship('UseCaseModel')
 user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
 version_id = db.Column(db.Float, nullable=True)
 job_status = db.Column(ChoiceType(JOB_STATUS_TYPES))
 dataset_id = db.Column(db.Integer, db.ForeignKey('datasets.dataset_id'), nullable=False)
 dataset = db.relationship('DataSetModel')
 training_details = db.Column(JSON)
 testing_details = db.Column(JSON)

这是我执行迁移命令后的文件2easaw23523sa.py

from alembic import op
import sqlalchemy as sa
from mi import mig
def upgrade():
   op.create_table('jobs',
   sa.Column('job_id', sa.Integer(), nullable=False),
   sa.Column('usecase_id', sa.Integer(), nullable=False),
   sa.Column('user_id', sa.Integer(), nullable=False),
   sa.Column('version_id', sa.Float(), nullable=True),
   sa.Column('job_status', mig.ChoiceType(length=255), nullable=True),
   sa.Column('dataset_id', sa.Integer(), nullable=False),
   sa.Column('training_details', sa.JSON(), nullable=True),
   sa.Column('testing_details', sa.JSON(), nullable=True),
   sa.ForeignKeyConstraint(['dataset_id'], ['datasets.dataset_id'], ),
   sa.ForeignKeyConstraint(['usecase_id'], ['use_cases.id'], ),
   sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
   sa.PrimaryKeyConstraint('job_id')
   )

def downgrade():
   # ### commands auto generated by Alembic - please adjust! ###
   op.drop_table('jobs')

# ### end Alembic commands ###

Note - 语句 from mi import mig 出现是因为如果使用了中给出的过程这个答案

您可以通过将 length=255 替换为 {1:2}(或任何其他 python Truthy 对象来解决此问题,并且可以传递到 dict() 而不会引发异常).

即替换行

sa.Column('job_status', mig.ChoiceType(length=255), nullable=True),

sa.Column('job_status', mig.ChoiceType({1:2}), nullable=True),