Opened 9 years ago

Last modified 9 years ago

#25312 closed Bug

Bug: AutoField backed by Postgres 9+ creates error condition — at Initial Version

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

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

fitcode_app=> alter sequence eventlogs_eventname_id_seq restart with 4294967297;
ALTER SEQUENCE
fitcode_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 (0)

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