Opened 17 years ago

Closed 17 years ago

#7057 closed (fixed)

ORA-00918: column ambiguously defined error

Reported by: Erin Kelly Owned by: nobody
Component: Database layer (models, ORM) Version: queryset-refactor
Severity: 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

Column aliases aren't being generated for columns with conflicting names. This causes problems in Oracle when doing pagination, which effectively wraps the query in "SELECT * FROM (%s)". Here's an example, where the test_species.id and test_genus.id columns conflict, as well as the test_species.name and test_genus.name columns:

In [5]: qs = Species.objects.all().select_related(depth=1)[:5]

In [6]: qs.query.as_sql()
Out[6]:
('SELECT * FROM (SELECT (ROW_NUMBER() OVER (ORDER BY "TEST_SPECIES"."ID" )) AS rn, "TEST_SPECIES"."ID", "TEST_SPECIES"."NAME", "TEST_SPECIES"."GENUS_ID", "TEST_GENUS"."ID", "TEST_GENUS"."NAME", "TEST_GENUS"."FAMILY_ID", "TEST_SPECIES"."ID", "TEST_SPECIES"."NAME", "TEST_SPECIES"."GENUS_ID", "TEST_GENUS"."ID", "TEST_GENUS"."NAME", "TEST_GENUS"."FAMILY_ID" FROM "TEST_SPECIES" INNER JOIN "TEST_GENUS" ON ("TEST_SPECIES"."GENUS_ID" = "TEST_GENUS"."ID")) WHERE rn > 0 AND rn <= 5',
 ())

In [7]: list(qs)

...

<class 'cx_Oracle.DatabaseError'>: ORA-00918: column ambiguously defined

Attachments (1)

7057.diff (1.1 KB ) - added by Erin Kelly 17 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by Malcolm Tredinnick, 17 years ago

Triage Stage: UnreviewedAccepted

Aliasing the column names in the Oracle backend is one solution, but it means duplicating a lot of the get_columns() method to do so, or adding an extra do-nothing function call for the common case so that the Oracle backend can override it. Neither option immediately fills me with joy.

Is there anything particularly bad about explicitly writing all the table_name.column_name strings again in the outer select, rather than using select *? We have pretty easy access to those at the point the outer query is created, so it's probably only a couple of lines of code and even I should be able to manage to do that without screwing up.

Happy to be guided by the experts here.

comment:2 by Erin Kelly, 17 years ago

Unfortunately, I don't think that will work. The table names are no longer accessible in the outer select list. If the column names are specified, then they have to be qualified using the name of the subquery instead. So there's no way at that level to distinguish between the columns that were originally called species.name and genus.name.

comment:3 by Malcolm Tredinnick, 17 years ago

Aah, good point.

So Oracle just hates me, then. We need to allow column aliases in some cases. I'll do that today.

comment:4 by Malcolm Tredinnick, 17 years ago

Keywords: qs-rf removed

I've attempted to fix this in [7457] (the automatic commit message reference didn't come through to here, though). It looks like it's doing the right thing.

If it could be tested on a real Oracle server, it would be appreciated.

by Erin Kelly, 17 years ago

Attachment: 7057.diff added

comment:5 by Erin Kelly, 17 years ago

It works with the patch I just uploaded. Without that, there are still some conflicts since get_default_columns() doesn't update the col_aliases set.

comment:6 by Malcolm Tredinnick, 17 years ago

Thanks for the patch. Can't use it unmodified, since the partition() method on strings was only introduced in Python 2.5. But I can work around that fairly easily.

comment:7 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [7466]) queryset-refactor: Added a few modifications to the select column aliases from
[7457], based on a patch from Ian Kelly. Fixed #7057.

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