Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.4K
Visualizações
Cannot cast type integer to uuid

I tried to run python3 manage.py migrate, but i got this error:

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django /db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type integer to uuid
LINE 1: ...BLE "core_post" ALTER COLUMN "id" TYPE uuid USING "id"::uuid

I'm using PostgreSQL

My models.py: Link to pastebin

I hope you will help me)

Thank you!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

So, basically you started the model without an explicit primary key. So you got a free AutoField and now want them to be UUID's.

As you can see, Postgres (or any db I know of) doesn't allow changing integers to UUIDs. So you need do a few migrations to get the state you want to:

First add the uuid field as a normal field with a different name:

import uuid
from django.db import models

class MyModel(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, unique=True)

Run makemigrations and migrate.

Now make it primary key:

class MyModel(models.Model):
    uuid = models.UUIDField(default=uuid.uuid4, primary_key=True)

This should generate a migration that removes the id field and makes uuid primary key (makemigrations and migrate again).

And finally (if you still want to):

class MyModel(models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)

Honestly, I don't know if this goes OK, you may have to adjust the generated migration. In either case, you've should now be where you want to be.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda