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
.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With