#2144 closed defect (fixed)
[patch] parameter conversion failing with get_or_create....
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm having an issue with the get_or_create helper method. Apparently
it isn't coercing parameters properly.
I've got some code that looks like this:
e = Foo(name=name, category=c, display_name=name.capitalize())
e2, created = Foo.objects.get_or_create(name=name, category=c, display_name=name.capitalize())
The first one will work and the second fails. The issue being that
the category is a foreign key and when creating the Foo object it uses
the id of category. When using the get_or_create method, it passes in
the Category object as the SQL query parameter instead of the id.
Thus giving the following error:
File "scanfoo.py", line 60, in pop_db e2, created = Foo.objects.get_or_create(name=name, category=c, display_name=name.capitalize()) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/manager.py", line 69, in get_or_create return self.get_query_set().get_or_create(*args, **kwargs) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py", line 217, in get_or_create return self.get(**kwargs), False File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py", line 202, in get obj_list = list(clone) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py", line 94, in __iter__ return iter(self._get_data()) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py", line 412, in _get_data self._result_cache = list(self.iterator()) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/models/query.py", line 163, in iterator cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/backends/util.py", line 12, in execute return self.cursor.execute(sql, params) File "/home/matt/work/vpython/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/db/backends/sqlite3/base.py", line 79, in execute return Database.Cursor.execute(self, query, params) pysqlite2.dbapi2.InterfaceError: Error binding parameter 0 - probably unsupported type.
I've put some debugging statements in to confirm this.
The problem is in query.py line 877 where field.get_db_prep_lookup is called.
Adding the defaults parameter to get_or_create doesn't help either...
Attachments (1)
Change History (8)
comment:1 by , 19 years ago
comment:2 by , 18 years ago
I've resolved it by using the hack above, but any idea when it will be officially fixed in the trunk?
comment:3 by , 18 years ago
since Adrians recent rename of 'clause' to 'lookup_type' the above patch broke.
i've updated the patch and diffed it from my development version.
patchname: patch-django-db-models-query_parameter_convertion_#2144
by , 18 years ago
Attachment: | patch-django-db-models-query_parameter_convertion_#2144 added |
---|
comment:4 by , 18 years ago
Summary: | parameter conversion failing with get_or_create.... → [patch] parameter conversion failing with get_or_create.... |
---|
comment:5 by , 18 years ago
This patch is quite hackish -- I wouldn't be comfortable accepting it as-is.
comment:6 by , 18 years ago
tried the similar test with revision 3336 -- appears to work for me
was it fixed by any chance?
comment:7 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Adrian: the patch is very hackish :) and nothing more then an updated version of the org. hack.
Alexis: your right, i've checked the svn with the problem described above and the problem seems to be fixed in changeset 3246,
There russelm commits a much better and cleaner patch in get_db_prep_lookup to lookup the primary key value for an object
I hit this bug from another call then get_or_create (dont remember which one) but with exactly the same results.
Currently i can not reproduce the bug in my application with r3246 or higher.
Closing the ticket for now, since it got resolved way back in r3246 :)
If the patch later turns out to be a solution to a still existing bugn then i'm willing rewrite the patch into something exceptable.
I updated lookup_inner in query.py to workaround this like so...