# Error when I change id to UUID

**URL:** <https://forum.djangoproject.com/t/error-when-i-change-id-to-uuid/13661>\
**Category:** Using the ORM\
**Created:** [May 12, 2022, 5:03pm UTC](https://forum.djangoproject.com/t/error-when-i-change-id-to-uuid/13661 "2022-05-12T17:03:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lapita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lapita/32/8324_2.png) [@Lapita](https://forum.djangoproject.com/u/Lapita)\
**Post date:** [May 12, 2022, 5:03pm UTC](https://forum.djangoproject.com/t/error-when-i-change-id-to-uuid/13661/1 "2022-05-12T17:03:47Z")

</div>

Hi everybody,

I have this model:

```auto
class Usuarios(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    website = models.URLField(max_length=200, blank=True, default="")
.
.
.
.

```

I did makemigrations and migrate but then I wanted to use UUID instead id, so I changed the model to this:

```auto
class Usuarios(models.Model):
    id = models.UUIDField(primary_key = True,default = uuid.uuid4, editable = False)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    website = models.URLField(max_length=200, blank=True, default="")
.
.
.
.

```

But I get the next error:

```auto
django.db.utils.ProgrammingError: column "id" is of type bigint but expression is of type uuid
LINE 1: ...uineo", "picture", "created", "modified") VALUES ('1c99de28-...
                                                             ^
HINT: You will need to rewrite or cast the expression.

```

I delete migrations and did again makemigrations and migrate, but I have the same error.

Someone who knows why this happens or I have to delete my database even though I have information that I do not want to delete?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 12, 2022, 5:16pm UTC](https://forum.djangoproject.com/t/error-when-i-change-id-to-uuid/13661/2 "2022-05-12T17:16:35Z")

</div>

Changing the primary key in Django is problematic.

If I had to do this, I’d do it one of two ways:

- Directly at the database layer.
  - Create and populate the UUID field
  - Drop the `id` column
  - Assign the new UUID field as the primary key

-or-

- Create a new model with the UUID as the new primary key
- Copy the data to the new model
- Drop the original model
- Rename the new model to the same name as the original.

Note that it’s only this easy if there are no other tables that have foreign key references to this table. If there are, you’re going to need to change those foreign key references as well.

If there’s not much data you’re concerned with, the simplest process is to drop and recreate the table.

It’s up to you to decide which is going to be the easiest to do.

---

<div class="post-metadata">

**Author:** ![Nekidev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nekidev/32/11968_2.png) [@Nekidev](https://forum.djangoproject.com/u/Nekidev)\
**Post date:** [January 29, 2023, 9:03am UTC](https://forum.djangoproject.com/t/error-when-i-change-id-to-uuid/13661/3 "2023-01-29T09:03:39Z")

</div>

What I would do (and have done) was change from bigint to varchar and from varchar to uuid. You need to first change the id field to `CharField`, change all ids from numbers to hex uuids, and then make a migration from varchar to uuid, casting all ids to UUID.
