4

I faced this behavior, not sure it's a bug. When I use connection created from cx_Oracle, It is working as assumed. But when I use Django DB connections, It is giving result but not as expected.

import cx_Oracle
from django.db import connections
import pandas as pd

dsn_tns = cx_Oracle.makedsn('xx.x.xx.xxx', 'port', 'dbname')
cx_Oracle_conn = cx_Oracle.connect('user', 'pass', dsn_tns)

django_conn = connections["DB2"] # In django settings, I have a created "DB2" and passed the same parameters.

query = ''' 
SELECT (T1.ACCEPTED- T2.CANCELLED) AS "NET" FROM
(
SELECT ID, COUNT(1) AS ACCEPTED FROM ACCEPTED_TABLE
GROUP BY ID
) T1
LEFT JOIN
(SELECT ID, COUNT(1) AS CANCELLED FROM CANCELLED_TABLE
GROUP BY ID) T2
ON T1.ID=T2.ID
'''

df1 = pd.read_sql(query, cx_Oracle_conn) # Gives right result
df2 = pd.read_sql(query, django_conn) # Gives only count of T1.ACCEPTED(T2.CANCELLED having 0 count)

Questions:

  1. What is the reason for this? How a connection can effect the execution of query?
  2. Is it a bug of Django? Or Do I need to use django connections in other way?
  3. How django.db.backends.oracle this works? Can explain in detail?

Versions:

Django == 2.1.2

Python == 3.6.7

'ENGINE': 'django.db.backends.oracle' # As mentioned in django document in DB connections
5
  • Are you sure, DB2 points to the same place than cx_Oracle_conn? Commented Jun 12, 2019 at 13:25
  • @trinchet Yes, I have given same credentials for both connections. And DB2 is DATABASE name provided in settings.py file. In general, I use the django connections. After facing this wired issue, I tried with normal procedure for connection. Commented Jun 12, 2019 at 17:27
  • Are other queries returning unexpected results against the django connection, or just the one in the original post? To triple-check that both connections are pointing to the same place, you could change the query to this: SELECT user||';'||SYS_CONTEXT('USERENV', 'DB_NAME')||';'||SYS_CONTEXT('USERENV', 'SERVER_HOST') from dual; Also, this post stackoverflow.com/a/28693089/11611377 suggests a different method of populating the DataFrame using read_sql_query and a QuerySet rather than read_sql. Have you tried that? Commented Jun 13, 2019 at 22:31
  • @cdub No, Django connection is working fine for other queries. Only for original post it is returning unexpected results. And I have also checked from idle by passing same parameters and to both connections. I got same unexpected results. In the link, django has models. But in my case I didn't created any models. So, I already have a query as string. And I have also tried with read_sql_query(No use). Commented Jun 14, 2019 at 4:31
  • It would be helpful to see whether the SQL that's being parsed by Oracle differs between the two connections. You can usually use the v$session and v$sql views to find the parsed queries - or work with your DBA to trace your sessions. If the SQL is the same between the two, then I'm out of ideas. Commented Jun 14, 2019 at 4:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.