#278 closed defect (fixed)
Problems saving objects with non-id primary key fields and SQLite database
| Reported by: | Owned by: | Adrian Holovaty | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.0 | 
| Severity: | normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
There are several problems using models which have a non-id primary_key field, when using a SQLite backend. Can't test this with other databases, unfortunately. If we have a python file like:
from django.core import meta
class D(meta.Model):
    fields = (
        meta.CharField('b', maxlength=8, primary_key=True),
        meta.CharField('c', maxlength=8),
    )
class B(meta.Model):
    fields = (
        meta.CharField('b', maxlength=8),
        meta.CharField('c', maxlength=8),
    )
then we get this problem: saving objects doesn't appear to add them to the database:
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from django.models.petrolprices import ds, bs >>> d = ds.D(b="a", c="a") >>> b = bs.B(b="a", c="a") >>> ds.get_list() [] >>> bs.get_list() [] >>> d.save() >>> b.save() >>> ds.get_list() [] >>> bs.get_list() [<B object>]
If, however, we have the python file containing this:
class D(meta.Model):
    fields = (
        meta.CharField('b', maxlength=8, primary_key=True),
    )
class B(meta.Model):
    fields = (
        meta.CharField('b', maxlength=45),
    )
Then attempting to save D objects gets a stack trace:
>>> from django.models.petrolprices import ds, bs
>>> d = ds.D(b='a')
>>> b = bs.B(b='a')
>>> ds.get_list()
[]
>>> bs.get_list()
[]
>>> b.save()
>>> bs.get_list()
[<B object>]
>>> d.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/Users/huw/Source/Django/django/utils/functional.py", line 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
  File "/Users/huw/Source/Django/django/core/meta/__init__.py", line 740, in method_save
    opts.pk.name), db_values + [getattr(self, opts.pk.name)])
  File "/Users/huw/Source/Django/django/core/db/base.py", line 10, in execute
    result = self.cursor.execute(sql, params)
  File "/Users/huw/Source/Django/django/core/db/backends/sqlite3.py", line 67, in execute
    return Database.Cursor.execute(self, query, params)
pysqlite2.dbapi2.OperationalError: near "WHERE": syntax error
      
  Note:
 See   TracTickets
 for help on using tickets.
    
I believe this is fixed by now. Please reopen if it's still a problem.