Nagarjuna
Nagarjuna

Reputation: 33

configure the django with Oracle 11g data base issue

Oracle database configurations with Django and while migrating the application facing the error

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the dja ngo_migrations table (ORA-02000: missing ALWAYS keyword)

application environment 
1.windows10
2.Python 3.6.x
3.Django 2.0.2
4.oracle 11g XE
in settins.py file 
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.oracle',
    'NAME': 'xe',
    'USER': 'abc',
    'PASSWORD':'xxxx',
    'HOST':'localhost',
    'PORT':"1521",

}

Upvotes: 3

Views: 7000

Answers (2)

Rajkumar Srinivasan
Rajkumar Srinivasan

Reputation: 128

remove self.cursor.numbersAsStrings = True in the above mentioned code

Upvotes: 6

Jose Constenla
Jose Constenla

Reputation: 121

The problem is that Django 2.0.2 only supports oracle 12g. Check this:

How to make Django 2.0 to use Oracle 11g syntax instead of 12c?

Also, you can check the sql failing, as pointed in the following question (adding to the manage.py a print(query) line)

Unable to create the django_migrations table (ORA-02000: missing ALWAYS keyword)

I've downgrade to Django 1.11 as recommended in the first question, but this leaded me to the error "AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'" because I have installed the last cx_Oracle version. (more information here: https://code.djangoproject.com/ticket/28138)

To fix this, I've modify the file C:\Program Files\Python37\lib\site-packages\django\db\backends\oracle\base.py to this:

def __init__(self, connection):
     self.cursor = connection.cursor()
     # Necessary to retrieve decimal values without rounding error.
     self.cursor.numbersAsStrings = True
     self.cursor.outputtypehandler = self._output_type_handler
     # Default arraysize of 1 is highly sub-optimal.
     self.cursor.arraysize = 100
     # https://github.com/django/django/commit/d52577b62b3138674807ac74251fab7faed48331

 @staticmethod
 def _output_type_handler(cursor, name, defaultType, length, precision, scale):
     """
     Called for each db column fetched from cursors. Return numbers as
     strings so that decimal values don't have rounding error.
     """
     if defaultType == Database.NUMBER:
         return cursor.var(
             Database.STRING,
             size=255,
             arraysize=cursor.arraysize,
             outconverter=str,
         )

I've take this code block from here:

https://github.com/cloudera/hue/commit/07d85f46eeec9c8c19d9aa11d131638e2a99e65c#diff-6d9bd161753aad635c23c2e91efafe91

With this, I've been able to migrate the project, at least. I don't know if it will fail while going further.

Hope this helps!

PD: I think your DATABASES setting should be as in http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/python_django/python_django.htm

DATABASES = {
'default': {
    'ENGINE':   'django.db.backends.oracle',
    'NAME':     'localhost/orcl',
    'USER':     'pythonhol',
    'PASSWORD': 'welcome',
}}

Upvotes: 6

Related Questions