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.
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>
* owner: nobody => Bhuvnesh
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:7>
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>
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>
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>
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>
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>
* 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>
* owner: (none) => Ayush Bisht
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:14>
* owner: Ayush Bisht => Priyank Panchal
--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:15>
* cc: Sarah Abderemane (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:16>
* owner: Priyank Panchal => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/34151#comment:17>