Reputation: 53
Getting django.db.utils.DatabaseError: ORA-00918: column ambiguously defined error when I try to open Django admin page. Here is the error:
raise dj_exc_value.with_traceback(traceback) from exc_value File "/venomscribe/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/venomscribe/lib/python3.6/site-packages/django/db/backends/oracle/base.py", line 514, in execute return self.cursor.execute(query, self._param_generator(params)) django.db.utils.DatabaseError: ORA-00918: column ambiguously defined
Query is
SELECT "DJANGO_ADMIN_LOG"."ID",
"DJANGO_ADMIN_LOG"."ACTION_TIME",
"DJANGO_ADMIN_LOG"."USER_ID",
"DJANGO_ADMIN_LOG"."CONTENT_TYPE_ID",
"DJANGO_ADMIN_LOG"."OBJECT_ID",
"DJANGO_ADMIN_LOG"."OBJECT_REPR",
"DJANGO_ADMIN_LOG"."ACTION_FLAG",
"DJANGO_ADMIN_LOG"."CHANGE_MESSAGE",
"AUTH_USER"."ID",
"AUTH_USER"."PASSWORD",
"AUTH_USER"."LAST_LOGIN",
"AUTH_USER"."IS_SUPERUSER",
"AUTH_USER"."USERNAME",
"AUTH_USER"."FIRST_NAME",
"AUTH_USER"."LAST_NAME",
"AUTH_USER"."EMAIL",
"AUTH_USER"."IS_STAFF",
"AUTH_USER"."IS_ACTIVE",
"AUTH_USER"."DATE_JOINED",
"DJANGO_CONTENT_TYPE"."ID",
"DJANGO_CONTENT_TYPE"."APP_LABEL",
"DJANGO_CONTENT_TYPE"."MODEL"
FROM "DJANGO_ADMIN_LOG"
INNER JOIN "AUTH_USER"
ON ("DJANGO_ADMIN_LOG"."USER_ID" = "AUTH_USER"."ID")
LEFT OUTER JOIN "DJANGO_CONTENT_TYPE"
ON ("DJANGO_ADMIN_LOG"."CONTENT_TYPE_ID" = "DJANGO_CONTENT_TYPE"."ID")
WHERE "DJANGO_ADMIN_LOG"."USER_ID" = :arg0
ORDER BY "DJANGO_ADMIN_LOG"."ACTION_TIME" DESC
FETCH FIRST 10 ROWS ONLY
Upvotes: 0
Views: 881
Reputation: 88
I got this error, using Oracle 12c.
If someone else gets this error, I resolved it by overriding the template of Django and removing the block of logs.
This error occurs in the file django/contrib/admin/templates/admin/index.html:26
, from Django.
This line call the function get_admin_log
, where this error is raised. By removing this block, you can render the Django Admin homepage normally.
Here is the Django documentation on overriding templates: https://docs.djangoproject.com/en/3.2/howto/overriding-templates/
Basicly, it is necessary to specify your template folder in setting.py
, and inside the templates folder, create the admin/index.html
file, and place the content from the original index, as shown below:
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}">{% endblock %}
{% block coltype %}colMS{% endblock %}
{% block bodyclass %}{{ block.super }} dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block nav-sidebar %}{% endblock %}
{% block content %}
<div id="content-main">
{% include "admin/app_list.html" with app_list=app_list show_changelinks=True %}
</div>
{% endblock %}
{% block sidebar %}
<!-- REMOVE THE CODE FROM HERE -->
<!-- django/contrib/admin/templates/admin/index.html -->
{% endblock %}
*Note: It is necessary to remove the code; if you simply comment it with HTML comments, Django/Jinja will continue to call the function.
Another option is to recreate the get_admin_log
function to work around this problem and retain the history."
Upvotes: 1
Reputation: 53
It was system requirement issue. I was using oracle 12.1 which is not compatible with django 3.0.3. Downgrading to 2.2 resolved the issue.
Upvotes: 1
Reputation: 50047
You're fetching DJANGO_ADMIN_LOG.ID, AUTH_USER.ID, and DJANGO_CONTENT_TYPE.ID, so you're trying to have three ID columns in your result set. This is not allowed. I suggest you alias these columns so you can distinguish between them:
SELECT l.ID AS DJANGO_ADMIN_LOG_ID,
l.ACTION_TIME,
l.USER_ID,
l.CONTENT_TYPE_ID,
l.OBJECT_ID,
l.OBJECT_REPR,
l.ACTION_FLAG,
l.CHANGE_MESSAGE,
u.ID AS AUTH_USER_ID,
u.PASSWORD,
u.LAST_LOGIN,
u.IS_SUPERUSER,
u.USERNAME,
u.FIRST_NAME,
u.LAST_NAME,
u.EMAIL,
u.IS_STAFF,
u.IS_ACTIVE,
u.DATE_JOINED,
t.ID AS DJANGO_CONTENT_TYPE_ID,
t.APP_LABEL,
t.MODEL
FROM DJANGO_ADMIN_LOG l
INNER JOIN AUTH_USER u
ON l.USER_ID = u.ID
LEFT OUTER JOIN DJANGO_CONTENT_TYPE t
ON l.CONTENT_TYPE_ID = t.ID
WHERE l.USER_ID = :arg0
ORDER BY l.ACTION_TIME DESC
FETCH FIRST 10 ROWS ONLY
I also suggest you use white space and table aliases to make the query easier to read.
Upvotes: 0