28π
As pointed out in this answer:
"A better approach is usually to add a uuid column, then fix up any foreign key references to point to it, and finally drop the original column."
So, to handle this in django, do the following:
1) revert migrations to a working graph
2) add temp_id = models.UUIDField(default=uuid.uuid4)
to your model, then run makemigrations
3) * add primary_key=True
to the temp_id
field, then run makemigrations
again
4) rename the field to id
(or to whatever you want), then run makemigrations
a third time
5) push the migrations to the database via python3 manage.py migrate
*This example assumes you donβt have data in the model you are changing. If you do, you will have to add a custom migration
in step 3.
Note that this was tested using django 3.1
and postgres
; YMMV
2π
After Hours, I Solved the Problem
With an really easy and simple way
According to this Post https://stackoverflow.com/a/69315948/9272467
Just Delete all your migrations files in the migration directory
Since converting the BigInt to UUID is the issue, just donβt make BigInt in the first place, it wonβt have any issue to create it UUID π
- Django admin β group permissions to edit or view models
- Drawing graphs in Django
- CSRF is only checked when authenticated in DRF?
- Django: Auto-generating a list of files in a directory
0π
I think you should define the default to UUID generator.
example:
id=models.UUIDField(primary_key=True,default=uuid.uuid4,unique=True)
- What cheat sheets exist for Django?
- Google App Engine and Cloud SQL: Lost connection to MySQL server at 'reading initial communication packet'
0π
I experienced the same problem when trying to use uuid
as id
on my djago model (django 3.2) with postgres.
I solved it by adding "max_length=36
" β which will limit the length of a field.
Related question and answer: Link
0π
If you donβt have any references to target table (or they can be dropped without pain), there is a bit easier approach, than described by @LordElrond:
-
Add
id = UUIDField(...)
-
Run
makemigrations
-
Open created migration and manually change
migrations.AlterField( model_name="mymodel", name="id", field=models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False), ),
to
migrations.RemoveField( model_name="mymodel", name="id", ), migrations.AddField( model_name="mymodel", name="id", field=models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False), ),
I guess, django could be doing it internally if no references to target table detected?
- After login the `rest-auth`, how to return more information?
- Django: Display a custom error message for admin validation error
0π
Follow these simple steps if you DONβT have important data in the database.
First, set the field as a CharField
and run ./manage.py makemigrations
.
id = CharField(primary_key=True, default=uuid.uuid4, editable=False)
Then update the CharField
to UUIDField
and run ./manage.py makemigrations
id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Finally, run ./manage.py migrate
. Working on Django 4.2
- Where is Pip3 Installing Modules?
- How to make trailing slash optional in django
- Is it possible to force queryset evaluation while keeping it a queryset
-1π
I had the same problem and solved it by simply manually changing the type of the id column in the table by replacing in with bigint
I hope it will be useful to whom this topic has touched.