Re: [Django] #34151: Adding explicit primary key with different type doesn't update related ManyToManyFields.

15 views
Skip to first unread message

Django

unread,
Nov 11, 2022, 12:14:57 PM11/11/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bhuvnesh):

IMO solving [https://code.djangoproject.com/ticket/29574 ticket-29574]
will also solve this issue.

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

Django

unread,
Nov 11, 2022, 12:36:12 PM11/11/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by STANISLAV LEPEKHOV):

Replying to [comment:4 Mariusz Felisiak]:
Thanks for the comments!
I wanted to note that the problem also occurs for the models.ForeignKey
fields.
The migration succeeds (in the absence of links), and after adding the
link, an exception is thrown.
I apologize in advance for the machine translation =)

Here is the code that will throw the exception:

{{{
class StoreChain(models.Model):
places = models.ManyToManyField(Place, blank=True)

class Place(models.Model):
pass
}}}
if change to this edition (set pk with uuid type) and add relation after:
{{{
class StoreChain(models.Model):
uid = models.UUIDField(default=uuid.uuid4, editable=False,
primary_key=True, unique=True)
place = models.ForeignKey (Place, blank=True, null=True)

class Place(models.Model):
uid = models.UUIDField(default=uuid.uuid4, editable=False,
primary_key=True, unique=True)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:6>

Django

unread,
Nov 22, 2022, 2:53:47 PM11/22/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned

Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bhuvnesh):

* owner: nobody => Bhuvnesh
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:7>

Django

unread,
Nov 23, 2022, 8:24:33 AM11/23/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Aravind Swaminathan):

Does this problem occur only while using a uuid field or even with other
type fields as primary key?

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:8>

Django

unread,
Nov 23, 2022, 11:02:47 PM11/23/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by STANISLAV LEPEKHOV):

Replying to [comment:8 Aravind Swaminathan]:


> Does this problem occur only while using a uuid field or even with other
type fields as primary key?

I found an error when changing the type to UUID, I did not check with
other types of fields.

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:9>

Django

unread,
Nov 29, 2022, 8:50:12 AM11/29/22
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bhuvnesh):

The migration generated for this contains removing the old pk and adding
new pk :
{{{
class Migration(migrations.Migration):

dependencies = [
("ticket_34151", "0001_initial"),
]

operations = [
migrations.RemoveField(
model_name="place",
name="id",
),
migrations.RemoveField(
model_name="storechain",
name="id",
),
migrations.AddField(
model_name="place",
name="uid",
field=models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
migrations.AddField(
model_name="storechain",
name="uid",
field=models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
]
}}}
but on some databases (mysql, postgres) we cannot delete a pk of a model
which is referenced by other.
I tried to implement a
solution([https://github.com/django/django/pull/16325 draft PR] ) which is
working fine for sqlite3 but throws the following error on some dbs
(mysql, postgres) during the RemoveField operation:
{{{
(1829, "Cannot drop column 'id': needed in a foreign key constraint
'ticket_34151_storech_placespk_id_ca7ce831_fk_ticket_34' of table
'ticket_34151_storechain'")
}}}
This made me think if it is really possible to add a new pk to a
referenced model for dbs like mysql and postgres?

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:10>

Django

unread,
Feb 13, 2023, 9:37:15 AM2/13/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Bhuvnesh):

STANISLAV, With a default uuid value you cannot insert previous values of
id in the new table ? So you can only change pk to uuid field when the db
is empty ? Correct me if i'm wrong.

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:11>

Django

unread,
Feb 14, 2023, 11:20:11 AM2/14/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by STANISLAV LEPEKHOV):

Replying to [comment:11 Bhuvnesh]:


> STANISLAV, With a default uuid value you cannot insert previous values
of id in the new table ? So you can only change pk to uuid field when the
db is empty ? Correct me if i'm wrong.

The essence of the problem is that I cannot correctly change the DATA TYPE
for the pk field (including with an empty database). If I have an integer
data type and there is a m2m field in another model containing pk of these
objects, then when changing the data type to uuid, there will be no
changes in the m2m table after migration (the type int, int will remain
there)

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:12>

Django

unread,
Feb 15, 2023, 1:31:53 PM2/15/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: (none)
Type: Bug | Status: new

Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Bhuvnesh):

* owner: Bhuvnesh => (none)
* status: assigned => new


Comment:

OK, so i did some more work on this and found that it is much more
complicated than i thought it would be:

1. You CANNOT switch pk field type if you already have some data in it
without custom operations.
2. the naive solution could be to remake all the tables that are
referencing to a model's pk but even that is not possible in MySql as we
cannot drop a table if it is referenced by other table.(throws error in
case when 2 models are related to each other)
3. the only solution i can think of (and tried) is deleting all related
models and then recreating them with new uid field.

So i'm deassigning myself in case someone has a better solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:13>

Django

unread,
Mar 11, 2023, 1:41:20 PM3/11/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Ayush
| Bisht
Type: Bug | Status: assigned

Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ayush Bisht):

* owner: (none) => Ayush Bisht


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:14>

Django

unread,
Apr 29, 2023, 1:37:55 AM4/29/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Priyank
| Panchal

Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Priyank Panchal):

* owner: Ayush Bisht => Priyank Panchal


--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:15>

Django

unread,
May 8, 2023, 7:04:00 AM5/8/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Priyank
| Panchal
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Abderemane):

* cc: Sarah Abderemane (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:16>

Django

unread,
Aug 16, 2023, 8:11:58 AM8/16/23
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Priyank Panchal):

* owner: Priyank Panchal => (none)


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:17>

Django

unread,
Feb 26, 2024, 11:38:01 PM2/26/24
to django-...@googlegroups.com
#34151: Adding explicit primary key with different type doesn't update related
ManyToManyFields.
-------------------------------------+-------------------------------------
Reporter: STANISLAV LEPEKHOV | Owner: Akash
| Kumar Sen
Type: Bug | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: migrations pk uuid | Triage Stage: Accepted
relation |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Akash Kumar Sen):

* owner: (none) => Akash Kumar Sen
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:18>
Reply all
Reply to author
Forward
0 new messages