drop unique constraint for sqlite

583 views
Skip to first unread message

Pan Luo

unread,
Dec 24, 2014, 1:40:21 PM12/24/14
to sqlalchem...@googlegroups.com
According to the doc here: http://alembic.readthedocs.org/en/latest/batch.html#including-unnamed-unique-constraints, sqlalchemy (<1.0) doesn't reflect the unique constraint. So when I drop it, I got: "ValueError: No such constraint: 'uq_Criteria_name'" error.  Here is my code:

convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}

with op.batch_alter_table('Criteria', naming_convention=convention) as batch_op:
    batch_op
.drop_constraint('uq_Criteria_name', type_='unique')

Where 'name' is the column in Criteria table. Did I missing anything? Is there any way to drop the unique constraint? Thanks.

Michael Bayer

unread,
Dec 26, 2014, 9:24:55 PM12/26/14
to sqlalchem...@googlegroups.com
if the constraint was not created with a name then it’s a little awkward to start using the naming convention feature after the fact.   

Also, if you’re only dealing with sqlite, you could just exclude the drop_constraint entirely as SQLAlchemy isn’t picking up on it and the recreate will just skip it.

Otherwise, since an unnamed constraint is not reflected in 0.9 you need to add it manually as shown at http://alembic.readthedocs.org/en/latest/batch.html#including-unnamed-unique-constraints.


Reply all
Reply to author
Forward
0 new messages