Search code examples
djangooracle-databaseinspectdb

How do I run inspectdb against different schemas in oracle?


I want to run inspectdb against an Oracle database. The user account that I use is a read only user that owns 0 tables. It, however, has access to the schema that contains all the tables. How do I specify a schema when using inspectdb?

My command, currently, is: python manage.py inspectdb --database "oradb" > test_model.py

This only outputs a file with from django.db import models.


Solution

  • César's answer is true regarding Oracle support. However, I was able to generate a rough model by modifying django\db\backends\oracle\introspection.py in two places.

    Change the cursor.execute line in get_table_list to read:

    cursor.execute("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'OTHERSCHEMA'")
    

    Change the user_table_cols to ALL_TAB_COLUMNS in the first cursor.execute line in get_table_description to read:

    cursor.execute("""
            SELECT
                column_name,
                data_default,
                CASE
                    WHEN char_used IS NULL THEN data_length
                    ELSE char_length
                END as internal_size
            FROM ALL_TAB_COLUMNS
            WHERE table_name = UPPER(%s)""", [table_name])
    

    Next, change the second cursor.execute line in get_table_description to read: cursor.execute("SELECT * FROM OTHERSCHEMA.%s WHERE ROWNUM < 2" % self.connection.ops.quote_name(table_name))

    Since this is a legacy database where policy prevents changes to the database, this was good for a one time run.

    A few more changes are needed once the model is complete. It appears a few of my classes are missing primary key references and foreign key references. I will add these manually.

    The last change I made was to modify all of the class Meta: instances to reference the correct schema:

    class Meta:
        db_table = u'"SCHEMA"."TABLE_NAME"'     # Notice the quoting needed