Emil Haas
Emil Haas

Reputation: 804

flask db migrate do not change column setting

I am using sql-alchemy with flask-migrate. I set up my database class with sth like this:

class Candidate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), index=False, unique=False)
    interviewer = db.Column(db.String(32), index=False, unique=False)

Unfortunately some of values was too long so I got the error:

sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation) value too long for type character varying(32)

Wasnt sure in which column was the problem (actual class is much more complicated) I changed the class like this:

class Candidate(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=False, unique=False)
    interviewer = db.Column(db.String(64), index=False, unique=False)

and then ran:

flask db migrate
flask db upgrade

to apply changes. Unfortunately I am still receiving the same error. What am I doing wrong?

Upvotes: 4

Views: 1714

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67527

Alembic (the migration engine behind Flask-Migrate) does not put data type changes in migrations by default, so your change from 32 to 64 was probably ignored. You can confirm by looking at the generated migration script.

To configure Alembic to watch for column type changes, you have to add the compare_type=True option when you create the Migrate class:

migrate = Migrate(app, db, compare_type=True)

After you do this, regenerate your migration, and it should have the code to make your columns larger.

Upvotes: 9

Related Questions