Django

Code

Ticket #7057 (closed: fixed)

Opened 5 months ago

Last modified 4 months ago

ORA-00918: column ambiguously defined error

Reported by: ikelly Assigned to: nobody
Milestone: Component: Database wrapper
Version: queryset-refactor Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

7057.diff (1.1 kB) - added by ikelly on 04/25/08 14:35:29.

Change History

04/22/08 12:30:33 changed by mtredinnick

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

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.

04/22/08 14:37:54 changed by ikelly

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.

04/22/08 23:13:14 changed by mtredinnick

Aah, good point.

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

04/25/08 01:57:06 changed by mtredinnick

  • keywords deleted.

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.

04/25/08 14:35:29 changed by ikelly

  • attachment 7057.diff added.

04/25/08 14:37:36 changed by ikelly

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.

04/25/08 21:35:05 changed by mtredinnick

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.

04/25/08 23:03:26 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #7057 (ORA-00918: column ambiguously defined error)




Change Properties
Action