Thursday, 13 June 2019

Django database connection library issue

I faced an illogical issue. 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



from Django database connection library issue

No comments:

Post a Comment