Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.ProgrammingError: cannot cast type uuid to integer

I tried to upload my web to Heroku server and stuck at this ploblem

my error when I try to migrate my database

The above exception was the direct cause of the following exception:

    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type uuid to integer
LINE 1: ...ict_word" ALTER COLUMN "id" TYPE integer USING "id"::integer
                                                              ^

models.py

class Word(models.Model):
    id = models.UUIDField(primary_key=True, unique=True)
    esearch = models.TextField()
    eentry = models.TextField()
    tentry = models.TextField()
    ecat = models.TextField()
    ethai = models.TextField(null=True)
    esyn = models.TextField(null=True)
    eant = models.TextField(null=True)

    def __str__(self):
        return self.eentry

    def get_absolute_url(self):
        return reverse('word-detail', args=[str(self.id)])

does anybody know how to fix it

like image 289
Man raider Avatar asked Feb 17 '26 10:02

Man raider


2 Answers

As pointed out by Craig Ringer:

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

like image 199
Lord Elrond Avatar answered Feb 19 '26 00:02

Lord Elrond


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?

like image 41
oxfn Avatar answered Feb 19 '26 00:02

oxfn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!