Django’s autoincrement behaviour will only use the positive numbers by default.
If you’re concerned about accidental insertion of negative values, you can subclass the field or use a check constraint to limit the range as per my related blog post.
Okay, lets say i want 0-65535
smallint unsigned on mysql.
How can i achieve that?
With 2 Storage (Bytes) as per mysql documentation smallint unsigned.
Correct me if i’m wrong but the empty cells still consume storage in DB.
So if we have
1 to 9223372036854775807
or
-9223372036854775808to9223372036854775807
It is 2x the difference in storage space.
Ken is right that there’s no space saving from unsigned vs signed.
To use an autofield with small unsigned integers you’ll want to use a subclass. But it’s not a good idea.
The reason Django moved its default from 32 bit to 64 bit is the danger of ID exhaustion - running out of autoincrement ID’s. This typically takes down a site until its database can be migrated - several large companies have had large outages because of this, e.g. Basecamp and Sentry.
If you’re looking for the storage savings from using 2 bytes instead of 8 - they’re really small compared to the cost of storage. The tradeoff will basically never be worth it.
You’ll realize much larger storage savings using a compressed database format e.g. PostgreSQL on ZFS or MariaDB’s table compression. This will compress all your data, not just the autoincrement column, and can save you a bunch of disk space.