Opened 4 years ago

Closed 4 years ago

#25312 closed Bug (duplicate)

Database sequence for AutoField backed by Postgres 9+ allows larger values than the column

Reported by: Stephan Doliov Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
Severity: Normal Keywords: AutoField, Postgres, Sequences
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Stephan Doliov)

The essence of this bug report is that models.py creates an inherent conflict between default, autoincrementing id fields (AutoField) and what a postgres 9.x (9.4 in my case) assigns as the type for the auto incrementing field. When makemigration creates the sequence, the sequence is created as a big integer. However, the database table that uses the sequence (the id field) is created as an integer. So once the value of the sequence exceeds the maximum value of a 32 bit integer, the insert will fail.

Below is models.py code and some psql command line foo to illustrate the problem

import datetime
import hashlib
import os
from math import sqrt
from random import Random
from django.db import models
from django.db.models.query import Q
from django.utils.text import slugify
from djangoratings.fields import RatingField
from django_pgjson.fields import JsonBField
from django_images.models import Image as BaseImage
from django.utils import timezone

class EventName(models.Model):
  event_name = models.CharField(max_length=500, blank=False, null=False)
  event_desc = models.CharField(max_length=500, blank=False, null=True)
  event_creation_date = models.DateTimeField(auto_now_add=True,null=False)
  event_update_date = models.DateTimeField(auto_now=True,null=False)

  def __unicode__(self):
    return self.name

steves_app=> \d eventlogs_eventname_id_seq 
     Sequence "public.eventlogs_eventname_id_seq"
    Column     |  Type   |           Value            
---------------+---------+----------------------------
 sequence_name | name    | eventlogs_eventname_id_seq
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | f
Owned by: public.eventlogs_eventname.id

steves_app=> \d eventlogs_eventname
                                        Table "public.eventlogs_eventname"
       Column        |           Type           |                            Modifiers                             
---------------------+--------------------------+------------------------------------------------------------------
 id                  | integer                  | not null default nextval('eventlogs_eventname_id_seq'::regclass)
 event_name          | character varying(500)   | not null
 event_desc          | character varying(500)   | 
 event_creation_date | timestamp with time zone | not null
 event_update_date   | timestamp with time zone | not null
Indexes:
    "eventlogs_eventname_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "eventlogs_event" CONSTRAINT "eventlog_event_id_id_153f5722b6e1dffb_fk_eventlogs_eventname_id" FOREIGN KEY (event_id_id) REFERENCES eventlogs_eventname(id) DEFERRABLE INITIALLY DEFERRED

steves_app=> alter sequence eventlogs_eventname_id_seq restart with 4294967297;
ALTER SEQUENCE
steves_app=> insert into eventlogs_eventname(event_name,event_desc,event_creation_date,event_update_date) values ('foo','bar',now(),now());
ERROR:  integer out of range

Change History (2)

comment:1 Changed 4 years ago by Stephan Doliov

Description: modified (diff)

comment:2 Changed 4 years ago by Tim Graham

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed
Summary: Bug: AutoField backed by Postgres 9+ creates error conditionDatabase sequence for AutoField backed by Postgres 9+ allows larger values than the column

I think we can consider this a duplicate of #14286 which requests BigAutoField.

I don't think the fact that the sequence supports values larger than the column is a problem, but feel free to reopen with a rationale if necessary (also a patch demonstrating the fix to show that it's 1) feasible to fix and 2) not too complicated would be a big help). Thanks!

Note: See TracTickets for help on using tickets.
Back to Top