Opened 14 years ago

Closed 14 years ago

#14161 closed (fixed)

GeoDjango autofield makes transaction fail

Reported by: miguel.araujo.perez@… Owned by: nobody
Component: GIS Version: 1.2
Severity: Keywords: postGIS, autofield, geodjango
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi there,

This is my GeoDjango model:

from django.contrib.gis.db import models
from django.db.models import fields

class Spot(models.Model):
    idSpot = fields.AutoField(primary_key = True)

    code = models.CharField(max_length = 30)

    poly = models.PointField(spatial_index = True,
                            srid = 4326,
                            geography = True)

    objects = models.GeoManager()

When I do this:

python manage.py shell

from location.models import Spot
from django.contrib.gis.geos import Point
spot = Spot("park",Point(10,10))
spot.save()

What I get is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/map/mercurial/telollevo/src/telollevogis/<ipython console> in <module>()

/usr/local/lib/python2.6/dist-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using)
    433         if force_insert and force_update:
    434             raise ValueError("Cannot force both insert and updating in model saving.")
--> 435         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    436 
    437     save.alters_data = True

/usr/local/lib/python2.6/dist-packages/django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update, using)
    495                 # Determine whether a record with the primary key already exists.

    496                 if (force_update or (not force_insert and
--> 497                         manager.using(using).filter(pk=pk_val).exists())):
    498                     # It does already exist, so do an UPDATE.

    499                     if force_update or non_pks:

/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in filter(self, *args, **kwargs)
    548         set.
    549         """
--> 550         return self._filter_or_exclude(False, *args, **kwargs)
    551 
    552     def exclude(self, *args, **kwargs):

/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in _filter_or_exclude(self, negate, *args, **kwargs)
    566             clone.query.add_q(~Q(*args, **kwargs))
    567         else:
--> 568             clone.query.add_q(Q(*args, **kwargs))
    569         return clone
    570 

/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.pyc in add_q(self, q_object, used_aliases)
   1129                 else:
   1130                     self.add_filter(child, connector, q_object.negated,
-> 1131                             can_reuse=used_aliases)
   1132                 self.where.end_subtree()
   1133                 if connector == OR:

/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.pyc in add_filter(self, filter_expr, connector, negate, trim, can_reuse, process_extras)
   1069         else:
   1070             self.where.add((Constraint(alias, col, field), lookup_type, value),
-> 1071                 connector)
   1072 
   1073         if negate:

/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/sql/where.pyc in add(self, data, connector)
     39                  isinstance(obj.field, GeometryField) ):
     40                 data = (GeoConstraint(obj), lookup_type, value)
---> 41         super(GeoWhereNode, self).add(data, connector)
     42 
     43     def make_atom(self, child, qn, connection):

/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.pyc in add(self, data, connector)
     64 
     65         if hasattr(obj, "prepare"):
---> 66             value = obj.prepare(lookup_type, value)
     67             super(WhereNode, self).add((obj, lookup_type, annotation, value),
     68                 connector)

/usr/local/lib/python2.6/dist-packages/django/db/models/sql/where.pyc in prepare(self, lookup_type, value)
    297     def prepare(self, lookup_type, value):
    298         if self.field:
--> 299             return self.field.get_prep_lookup(lookup_type, value)
    300         return value
    301 

/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.pyc in get_prep_lookup(self, lookup_type, value)
    290             return value
    291         elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
--> 292             return self.get_prep_value(value)
    293         elif lookup_type in ('range', 'in'):
    294             return [self.get_prep_value(v) for v in value]

/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.pyc in get_prep_value(self, value)
    474         if value is None:
    475             return None
--> 476         return int(value)
    477 
    478     def contribute_to_class(self, cls, name):

ValueError: invalid literal for int() with base 10: 'parque'

Instead if I do:

spot = Spot(1,"park",Point(10,10))
spot.save()

Everything goes right if primary key 1 does not already exist. If I check table in the database, record is there. Also there is a sequence associated to the primary key.

location=# \d location_spot
                                         Tabla «public.location_spot»
 Columna |         Tipo          |                               Modificadores                                
---------+-----------------------+----------------------------------------------------------------------------
 idSpot  | integer               | not null valor por omisión nextval('"location_spot_idSpot_seq"'::regclass)
 code    | character varying(30) | not null
 poly    | geography(Point,4326) | not null
Índices:
    "location_spot_pkey" PRIMARY KEY, btree ("idSpot")
    "location_spot_poly_id" gist (poly)

Using autofield in Django doesn't work this way, so I think it might be a bug. I'm using:

  • Django 1.2.1 final
  • Latest GEOS version
  • Latest Proj version
  • PostGIS 1.5 latest version
  • Ubuntu Linux 9.10

Followed these instructions for installation http://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#building-from-source

Thanks, regards
Miguel Araujo

Change History (1)

comment:1 by miguel.araujo.perez@…, 14 years ago

Resolution: fixed
Status: newclosed

I'm look at this at the moment while I was debugging django code to see If I could see where the bug is and submit a patch.

The bug of course is in my code. I should be doing:

spot = Spot(code="park", poly=Point(10,10))
spot.save()

which I'm pretty sure I tried yesterday night and didn't work, but I guess I misspelled something and I didn't notice (late hacking). This is such dumb ticket. I'm very sorry about it, please delete it.

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