[Django] #32869: Bug while migrating a OneToOneField to ForeignKey on PostgreSQL.

15 views
Skip to first unread message

Django

unread,
Jun 21, 2021, 7:34:08 AM6/21/21
to django-...@googlegroups.com
#32869: Bug while migrating a OneToOneField to ForeignKey on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: suhailvs | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.2
Migrations | Keywords: postgres
Severity: Normal | onetoonefield foreignkey
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Using PostgreSQL Database I changed a field from `OneToOneField` to
`ForeignKey`::

{{{
from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
}}}

to

{{{
class Customer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
}}}

after migrations, then i tried to create a Customer i got error:

{{{
IntegrityError at /admin/myapp/customer/add/
duplicate key value violates unique constraint
"myapp_customer_user_id_key"
DETAIL: Key (user_id)=(1) already exists.
Request Method: POST
Request URL: http://localhost:8001/admin/myapp/customer/add/
Django Version: 3.2.4
Exception Type: IntegrityError
Exception Value:
duplicate key value violates unique constraint
"myapp_customer_user_id_key"
DETAIL: Key (user_id)=(1) already exists.
Exception Location:
/home/donams/suhail/github/temp/test_unique/env/lib/python3.6/site-
packages/django/db/backends/utils.py, line 84, in _execute
Python Executable:
/home/donams/suhail/github/temp/test_unique/env/bin/python
Python Version: 3.6.9
Python Path:
['/home/donams/suhail/github/temp/test_unique',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/donams/suhail/github/temp/test_unique/env/lib/python3.6/site-
packages']
Server time: Mon, 21 Jun 2021 11:22:55 +0000
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32869>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 21, 2021, 8:34:50 AM6/21/21
to django-...@googlegroups.com
#32869: Bug while migrating a OneToOneField to ForeignKey on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: suhailvs | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 3.2
Severity: Normal | Resolution:
| worksforme
Keywords: postgres | Triage Stage:
onetoonefield foreignkey | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => worksforme


Comment:

Thanks for the report, however, it works for me. Django drops a unique
constraint when altering `OneToOneField` to the `ForeignKey`, e.g.
{{{
$ python manage.py sqlmigrate test_32869 0002

BEGIN;
--
-- Alter field user on customer
--
SET CONSTRAINTS "test_32869_customer_user_id_794e3015_fk_auth_user_id"
IMMEDIATE; ALTER TABLE "test_32869_customer" DROP CONSTRAINT
"test_32869_customer_user_id_794e3015_fk_auth_user_id";
ALTER TABLE "test_32869_customer" DROP CONSTRAINT
"test_32869_customer_user_id_key";
CREATE INDEX "test_32869_customer_user_id_794e3015" ON
"test_32869_customer" ("user_id");
ALTER TABLE "test_32869_customer" ADD CONSTRAINT
"test_32869_customer_user_id_794e3015_fk_auth_user_id" FOREIGN KEY
("user_id") REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
}}}
(I used `test_32869` instead of `myapp`.)

--
Ticket URL: <https://code.djangoproject.com/ticket/32869#comment:1>

Reply all
Reply to author
Forward
0 new messages