tl;dr: Introduce new field types to handle auto-incremented ID fields, change 
the PostgreSQL backend to use the preferred syntax

--

One of the most common issues my company runs into on Django sites is that 
models.AutoField defaults to a 32-bit integer (int32).  2^31-1 possible entries 
is just not that many anymore, and by the time the developers realize this and 
need to move to a 64 bit integer key, it's too late to do so conveniently, 
because expanding the field is very painful (in PostgreSQL, at least).

While models.AutoBigField exists, it's barely mentioned in examples, and is 
often overlooked.

Changing AutoField to 64 bits would result in all kinds of breakage; at best, a 
lot of very unplanned and expensive migrations.

My proposal is:

1. Create two new field types to represent auto-incrementing primary keys.  I'd 
suggest IdentityField and SmallIdentityField for int64 and int32, respectively.

2. Change all examples to use SerialField instead of AutoField.

3. As a side note, switch the PostgreSQL backend to use the standard "GENERATED 
BY DEFAULT AS IDENTITY" syntax.  This became available in PostgreSQL version 
10, but the previous version (9.6) reaches EOL in November 2021.

4. At some point in the future, deprecate AutoField and AutoBigField.

This would result in new projects getting 64 bit primary keys by default.  I 
think that's a positive.  For small tables, the size difference hardly matters; 
for big tables, we have saved a major foot-gun of either integer exhaustion or 
a very expensive data migration problem.

--

Comments?

--
-- Christophe Pettus
   x...@thebuild.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/F4A50AB6-8CC1-4B6E-BD1B-B865F53A645A%40thebuild.com.

Reply via email to