Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 476
Emil Haas Avatar asked Oct 28 '25 14:10

Emil Haas


1 Answers

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.

like image 108
Miguel Avatar answered Oct 31 '25 04:10

Miguel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!