Opened 11 years ago

Closed 10 years ago

#20971 closed Bug (duplicate)

Annotating a count of distinct ForeignKey (a) of a reverse foreignkey (b) doesn't work on oracle if (a) has any TextFields

Reported by: kimvais@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: oracle
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Shai Berger)

If there is a model X that has foreignkey to model Y and X has foreign key to Z and has TextFields the query for an instance of Y fails.

E.g.
y.xys.all().defer(list_of_all_text_fields).annotate(Count('z__pk', distinct=True) causes "DatabaseError: ORA-00932: inconsistent datatypes: expected - got NCLOB"

This is probably(?) because the "GROUP BY" clause will contain all the deferred fields.

As a workaround I have to do sum(len(set(x)) for x in y.xys.all().values_list('z__pk'))
Works flawlessly on PostgreSQL.

Change History (5)

comment:1 by kimvais@…, 11 years ago

Looks related to #17144

comment:2 by Shai Berger, 11 years ago

Description: modified (diff)
Type: UncategorizedBug

This seems to be a correctness problem on Oracle, but may cause performance issues elsewhere (other backends will group over deferred fields, in particular text-fields, which may indeed bring back the problems of #17144).

What this needs now is a test-case. Note that if the test checks the generated SQL (this may be done with qset.query.sql_with_params()) it can be made to fail on non-oracle backends.

comment:3 by Shai Berger, 11 years ago

#19259 appears related.

comment:4 by Tim Graham, 11 years ago

Triage Stage: UnreviewedAccepted

comment:5 by Shai Berger, 10 years ago

Resolution: duplicate
Status: newclosed

#24096 is a more general (and simple) description of the problem, and has a test-case.

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