[Solved]-Django.db.utils.ProgrammingError: cannot cast type uuid to integer

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

πŸ‘€Lord Elrond

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 πŸ˜‰

πŸ‘€Peko Chan

0πŸ‘

I think you should define the default to UUID generator.

example:

id=models.UUIDField(primary_key=True,default=uuid.uuid4,unique=True)
πŸ‘€Richard Agrey

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

πŸ‘€Mikesiwi

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:

  1. Add id = UUIDField(...)

  2. Run makemigrations

  3. 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?

πŸ‘€oxfn

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

-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.

Leave a comment