#25312: Bug: AutoField backed by Postgres 9+ creates error condition
-------------------------------------+-------------------------------------
     Reporter:  infintesimal         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Uncategorized        |                  Version:  1.7
     Severity:  Normal               |               Resolution:
     Keywords:  AutoField,           |             Triage Stage:
  Postgres, Sequences                |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by infintesimal):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old 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
>
> }}}

New 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

 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

 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25312#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.5d92d1da06116d05bc076e6c059052da%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to