Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error at migrating in Flask with sqlalchemy_utils ChoiceType

I have a Flask model:

class User(db.Model):
    ROLE_USER = 0
    ROLE_MODERATOR = 1
    ROLE_ADMIN = 2
    ROLES = [
        (ROLE_USER, u'Regular user'),
        (ROLE_MODERATOR, u'Moderator'),
        (ROLE_ADMIN, u'Admin')
    ]

    id = db.Column(db.Integer, primary_key = True)
    login = db.Column(db.String(32), nullable=False, unique=True)
    first_name = db.Column(db.String(32))
    last_name = db.Column(db.String(32))
    role = db.Column(ChoiceType(ROLES), nullable=False)

And I've created an migration with flask-migrate (db is Postgresql):

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('login', sa.String(length=32), nullable=False),
    sa.Column('first_name', sa.String(length=32), nullable=True),
    sa.Column('last_name', sa.String(length=32), nullable=True),
    sa.Column('role', sqlalchemy_utils.types.choice.ChoiceType(length=255), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('login')
    )

Migration was created successfuly, but when I want to upgrade, this error rising:

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

Can somebody explain the problem?

like image 514
AKhimself Avatar asked Mar 29 '15 14:03

AKhimself


1 Answers

As the error says, ChoiceType does not have an init argument called length:

http://sqlalchemy-utils.readthedocs.org/en/latest/data_types.html#module-sqlalchemy_utils.types.choice

You can remove it and use

sqlalchemy_utils.types.choice.ChoiceType(User.ROLES)

instead.

like image 67
Selcuk Avatar answered Oct 11 '22 14:10

Selcuk