Flask-migrate: I'm trying to remove a unique=True from one of my columns.

Created on 12 Oct 2015  路  7Comments  路  Source: miguelgrinberg/Flask-Migrate

I'm trying to remove a unique=True from one of my columns. But I don't see any documentation on how to set unique=False in the docs.

I've come across some alembic docs but I'm still not sure if this is the proper way to do it.... And how am I supposed to find my constraint name? I assume that these unique constraint names were automatically assigned.

I followed the following source to drop the constraint but it seems that

op.drop_constraint("title",
    "book",
    "unique")

is not working ..

error message:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) constraint "title" of relation "book" does not exist.

Source:
http://skien.cc/blog/2014/01/31/adding-unique-contraints-after-the-fact-in-sqlalchemy/

question

Most helpful comment

If anyone else finds this it seems like the constraint is named TABLE_COLUMN_key.

Removing the unique constraint from the column name on the table experiments would look like this-

def upgrade():
    op.drop_constraint('experiments_name_key', 'experiments')

def downgrade():
    op.create_unique_constraint('experiments_name_key', 'experiments', ['name'])

All 7 comments

Did you look in the migration where this constraint was added? That should show you the name of your constraint, I think.

@miguelgrinberg

This is what I see...

...version.py
op.create_index(op.f('ix_book_title'), 'book', ['title'], unique=True)

I replaced the "title" with "ix_book_title" but it is saying that it doesn't see it

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) constraint "ix_book_title" of relation "book" does not exist
 [SQL: 'ALTER TABLE book DROP CONSTRAINT ix_book_title']

Did you try drop_index instead of drop_constraint?

I think you need to drop the index and the constraint...

Is it possible to detect this for future changes from unique=True to unique=False?

I know where like three years later but I'm hitting this issue now and am curious how it got solved.

If anyone else finds this it seems like the constraint is named TABLE_COLUMN_key.

Removing the unique constraint from the column name on the table experiments would look like this-

def upgrade():
    op.drop_constraint('experiments_name_key', 'experiments')

def downgrade():
    op.create_unique_constraint('experiments_name_key', 'experiments', ['name'])

i am also getting the same problem does anyone solved it yet ?

Was this page helpful?
0 / 5 - 0 ratings