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:
- What is the reason for this? How a connection can effect the execution of query?
- Is it a bug of Django? Or Do I need to use django connections in other way?
- How
django.db.backends.oraclethis 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
DB2points to the same place thancx_Oracle_conn?DB2is DATABASE name provided insettings.pyfile. In general, I use the django connections. After facing this wired issue, I tried with normal procedure for connection.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?read_sql_query(No use).