'ProgrammingError: cannot cast type bigint to uuid' in Django

I've been moving development of my website over to using Docker. I replaced sqlite as my database with postgresql then ran the command docker-compose exec web python manage.py migrate in my Docker environment and it produced the following error:

 File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.CannotCoerce: cannot cast type bigint to uuid
LINE 1: ...quirementschat" ALTER COLUMN "id" TYPE uuid USING "id"::uuid
                                                                 ^


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

Traceback (most recent call last):
  File "/code/manage.py", line 22, in <module>
    main()
  File "/code/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/usr/local/lib/python3.11/site-packages/django/db/migrations/operations/fields.py", line 235, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 830, in alter_field
    self._alter_field(
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 287, in _alter_field
    super()._alter_field(
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 1055, in _alter_field
    self.execute(
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/schema.py", line 48, in execute
    return super().execute(sql, None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/schema.py", line 201, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: cannot cast type bigint to uuid
LINE 1: ...quirementschat" ALTER COLUMN "id" TYPE uuid USING "id"::uuid
                                                                 ^

I haven't been able to find any working solutions. I renamed my 'id' field and ran migrations but still ended up with the same errors. I deleted all records in my database, changed the field from Charfield to UUIDField but the error persists.

Here is my models.py:

class RequirementsChat(models.Model):
    id = models.CharField(primary_key=True, max_length=38)
    #id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, max_length=37)
    alias = models.CharField(max_length=20, blank=True, null=True)
    email = models.CharField(max_length=80, blank=True, null=True)
    language = models.CharField(max_length=10, blank=True, null=True)
    due_date = models.CharField(max_length=10, blank=True, null=True)
    subtitle_type = models.CharField(max_length=10, blank=True, null=True)
    transcript_file_type = models.CharField(max_length=10, blank=True, null=True)
    additional_requirements = models.TextField(max_length=500, blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    url = models.CharField(max_length=250, blank=True, null=True)
    task_completed = models.BooleanField(default=False)
    
class UploadedFile(models.Model):
    input_file = models.FileField(upload_to='files')
    chat_id = models.CharField(max_length=33, null= True)
    requirements_chat = models.ForeignKey(RequirementsChat, on_delete=models.CASCADE, related_name='files', null=True)

Any suggestions?

You will have to

  1. Copy the data from the original filed in a temporary field
  2. Create a new UUID field
  3. Copy the data back from the temporary field into the new UUID field

For step 1

class RequirementsChat(models.Model):
    id_temp = models.UUIDField(default=uuid.uuid4, unique=True)
    ###

Then you have to makemigrations python manage.py makemigrations

Now step 2

from django.db import migrations

def copy_id_to_id_temp(apps, schema_editor):
    RequirementsChat = apps.get_model('yourappname', 'RequirementsChat')
    for chat in RequirementsChat.objects.all():
        chat.id_temp = uuid.UUID(chat.id)
        chat.save()

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', 'previous_migration'),
    ]

    operations = [
        migrations.RunPython(copy_id_to_id_temp),
    ]

Run the migration with python manage.py migrate

Step 3

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

Do a final makemigrations and migrate for the last time

Back to Top