# Unsigned Int Auto Increment Primary Keys

**URL:** <https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243>\
**Category:** Using the ORM\
**Created:** [April 16, 2024, 9:32pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243 "2024-04-16T21:32:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![UniqueConstraint](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/uniqueconstraint/32/20674_2.png) [@UniqueConstraint](https://forum.djangoproject.com/u/UniqueConstraint)\
**Post date:** [April 16, 2024, 9:32pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/1 "2024-04-16T21:32:25Z")

</div>

I’m migrating a legacy project with a large mysql (Mariadb) database, containing around 200 tables.

The existing database has unsigned int auto increment primary keys.

Foreign keys are also unsigned int.

How do I replicate this in Django, the autofield and bigautofield seem to be signed. Is this going to cause me issues?

I can set it like this, but there is no auto increment.

id = models.PositiveIntegerField(primary\_key=True)

---

<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:** [April 16, 2024, 9:50pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/2 "2024-04-16T21:50:52Z")

</div>

> [@UniqueConstraint](#):
>
> How do I replicate this in Django, the autofield and bigautofield seem to be signed. Is this going to cause me issues?

No, this isn’t going to create any issues.

---

<div class="post-metadata">

**Author:** ![UniqueConstraint](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/uniqueconstraint/32/20674_2.png) [@UniqueConstraint](https://forum.djangoproject.com/u/UniqueConstraint)\
**Post date:** [April 16, 2024, 10:08pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/3 "2024-04-16T22:08:52Z")

</div>

So how do I represent these unsigned auto increment primary key fields in a model?

---

<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:** [April 16, 2024, 10:15pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/4 "2024-04-16T22:15:35Z")

</div>

You don’t. You treat them as signed when you migrate them to the new database and don’t worry about it at that point.

---

<div class="post-metadata">

**Author:** ![UniqueConstraint](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/uniqueconstraint/32/20674_2.png) [@UniqueConstraint](https://forum.djangoproject.com/u/UniqueConstraint)\
**Post date:** [April 17, 2024, 5:30am UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/5 "2024-04-17T05:30:50Z")

</div>

So the code would think they are signed and I would lose half the capacity?

Would foreign key references I add to existing models would go in as the wrong type in managed migrations?

---

<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:** [April 17, 2024, 11:47am UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/6 "2024-04-17T11:47:23Z")

</div>

> [@UniqueConstraint](#):
>
> So the code would think they are signed and I would lose half the capacity?

The standard Django default for an id field is a `BigAutoField`. As an unsigned 63 bit field, its capacity is 9,223,372,036,854,775,807. I don’t think that’s going to be a problem.

> [@UniqueConstraint](#):
>
> Would foreign key references I add to existing models would go in as the wrong type in managed migrations?

I’m not following what you’re asking here.

You’re building a new application with new models. What you define in your new models are what’s going to be used.

All of the data will be saved as the new types when you migrate the data from the old tables to the new ones.

---

<div class="post-metadata">

**Author:** ![UniqueConstraint](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/uniqueconstraint/32/20674_2.png) [@UniqueConstraint](https://forum.djangoproject.com/u/UniqueConstraint)\
**Post date:** [April 17, 2024, 1:40pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/7 "2024-04-17T13:40:33Z")

</div>

I have migrated the existing database, with --fake-initial.

All my primary keys are unsigned ints.

If I try and make any foreign key references to existing tables it fails because it turns the foreign key into an AutoField value (i.e. signed), but the primary key it points to is still unsigned.

---

<div class="post-metadata">

**Author:** ![UniqueConstraint](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/uniqueconstraint/32/20674_2.png) [@UniqueConstraint](https://forum.djangoproject.com/u/UniqueConstraint)\
**Post date:** [April 17, 2024, 2:26pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/8 "2024-04-17T14:26:56Z")

</div>

The only way out of this I can see is to change all existing unsigned PKs to be signed. By doing so, I lose half the numeric capacity, or I use BigInt and double the storage requirements, plus cause myself a lot of hassle. Not ideal. Am I missing something?

---

<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:** [April 17, 2024, 2:55pm UTC](https://forum.djangoproject.com/t/unsigned-int-auto-increment-primary-keys/30243/9 "2024-04-17T14:55:14Z")

</div>

It depends upon how much data you’re talking about.

Half the range of an [AutoField](https://docs.djangoproject.com/en/5.0/ref/models/fields/#integerfield) is still \> 2,000,000,000 rows.

I find it highly unlikely that you’re going to encounter a situation with precisely \> 2,147,483,647 rows but less than twice that - which is the only case where this distinction matters.

> [@UniqueConstraint](#):
>
> plus cause myself a lot of hassle.

I see no additional hassle here either way.

If you’re creating a new Django project, you’re going to want to create new models _anyway_. This is just part of the upgrade process.
