Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 391
Andy Avatar asked Aug 06 '12 17:08

Andy


People also ask

Can Oracle database have multiple schemas?

In the Oracle database system, the term database schema, which is also known as "SQL schema," has a different meaning. Here, a database can have multiple schemas (or “schemata,” if you're feeling fancy). Each one contains all the objects created by a specific database user.

How do schemas work in Oracle?

A schema is a collection of database objects. A schema is owned by a database user and has the same name as that user. Schema objects are logical structures created by users. Objects such as tables or indexes hold data, or can consist of a definition only, such as a view or synonym.


1 Answers

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
like image 50
Andy Avatar answered Sep 22 '22 15:09

Andy