pythonsqlitesqlalchemyalembicflask-migrate

Is it possible to drop a unique constraint from a column?


I've tried simply removing the unique=True constraint and running

flask db migrate
flask db upgrade

in the command line but when I run my flask app I'm still getting a (sqlite3.IntegrityError) UNIQUE constraint failed error.

Is there a simple way to do this with flask-migrate or should I switch to alembic (which I know flask-migrate is just wrapped around)? I would prefer not to drop the entire table. Thanks for any help!


Solution

  • Flask-Migrate, or rather Alembic, will not autodetect anonymous constraints. But you can create a manual Alembic migration file to drop the constraint.

    Create an empty migration file:

    flask db revision -m 'Drop unique constraint'
    

    Edit the file to drop the constraint:

    def upgrade():
        op.drop_constraint("name_of_constraint", "table_name")
    
    def downgrade():
        op.create_index(...)
    

    I suggest you copy the create_index from the migration file that created the constraint in the first place, if possible.

    Then you can upgrade your database normally:

    flask db upgrade