#23740: Cannot drop unique_together constraint on a single field with its own
unique=True constraint
---------------------------------+------------------------------------
Reporter: Mihail Milushev | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: unique_together | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Comment (by Adrian Torres):
Just got hit by this bug, I believe that at least in recent versions of
Django (at least 3.2.13) it is now possible to distinguish `unique=True`
constraints from `unique_together` ones.
At least in the application I'm working on, there seem to be two different
naming schemes:
* For UNIQUE CONSTRAINT created with `unique=True` the naming scheme seems
to be `<table_name>_<field_name>_key`
* For UNIQUE CONSTRAINT created with `unique_together` the naming scheme
seems to be `<table_name>_<field_names>_<hash>_uniq`
Note that this is extrapolated from me looking at my application's
database schemas, I haven't read the code so I am unsure if this is in
fact what happens all of the time.
So in the case where we would want to delete a unique_together constraint
that had a single field, we'd just look for `foo_bar_%_uniq` and drop that
constraint, here is an SQL statement (in PostgreSQL) that I used in a
manual migration (originally from https://stackoverflow.com/a/12396684) in
case it can be useful to someone that comes across this bug again:
{{{
DO
$body$
DECLARE
_con text := (
SELECT quote_ident(conname)
FROM pg_constraint
WHERE conrelid = 'my_table'::regclass
AND contype = 'u'
AND conname LIKE 'my_table_my_field_%_uniq'
);
BEGIN
EXECUTE
'ALTER TABLE my_table DROP CONSTRAINT ' || _con;
END
$body$;
}}}
So I think it's possible to fix this bug now?
--
Ticket URL: <https://code.djangoproject.com/ticket/23740#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018075f2165e-4def8e04-2554-407a-a65f-1efaa05db8cf-000000%40eu-central-1.amazonses.com.