Tuesday, October 15, 2013

SQL cast for custom model lookup

I'm working with <https://github.com/ecometrica/django-dbarray>
and I'm wondering if there's a way to get django to add an explicit
cast to the SQL generated for field lookups.

I have a model that looks like this:

class Floats(models.Model):
arr = FloatArrayField()

Which translates to the following table definition in postgresql:

CREATE TABLE "dbarray_floats" (
"id" serial NOT NULL PRIMARY KEY,
"arr" double precision[]
)
;

When I evaluate something along these lines:

Floats.objects.filter(arr=[3.0, 9.0])

I get the following error:

DatabaseError: operator does not exist: double precision[] = numeric[]
LINE 1: ...ROM "dbarray_floats" WHERE "dbarray_floats"."arr" = ARRAY[3....
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

It looks to me like psycopg2 is for some reason presenting the query
parameter as numeric[], so I would need to somehow slip a type-cast
into the SQL statement so that the WHERE clause would look something
like this:

'WHERE "dbarray_floats"."arr" = %s::double precision[]'

But there doesn't seem to be any mechanism for a custom model field to
influence the way its placeholders are presented in SQL statements.

Interestingly, this is not a problem when saving array data, only
when performing lookups.

Is there any way either to get psycopg2's data adapter to present the
query parameters as double precision[] instead of numeric[] or to put
an explicit cast into the SQL, without rewriting django's sql
compiler?

I'm using Django 1.3 with psycopg 2.4.1 and PostgreSQL 9.1

Please cc: me on replies.

Regards,
Aryeh Leib

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20131015202008.GD1320%40localhost.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate