Can Django be prevented from truncating long table names?
I'm using Django with an existing Oracle DB (i.e., one where the tables were NOT created by Django). So in my models I am having to indicate the table name by specifying a value for db_table in the Meta class. I'm running into problems becuase the tables I wish to access belong to a different user than the one I have credentials for. I am authorized to view the tables (no trouble doing it in SQL Developer).
When the name of an Oracle table would otherwise be over 30 bytes, Django chops off the last four bytes of the name and replaces them with a repeatable 4-byte hash of the rest of the table’s name. This is all well and good for tables Django makes itself. It also normally wouldn’t be a problem for accessing tables in existing DBs (as in my case) since Oracle itself would’ve constrained t开发者_高级运维he names to 30 bytes.
The problem is that Django doesn’t have a separate facility for noting that the table belongs to another user. So I'm using a dot-syntax workaround (by just setting db_table as, e.g. “USERNAME.MY_29_BYTE_TABLE_NAMEXXXXXXXX”), but since this leads to the overall table name exceeding 30 bytes, Django does its truncation trick and tries to query against a table name that doesn’t exist.
Is there a way to prevent this behavior, or a different way to specify the user separate from the name of the table?
Truncate is caused at Oracle Django DB backend by quote_name method, which follows SQL92 requirements and uses hard-coded value max_name_length.
You can override this behavior by creating custom DB backend or by monkeypatch like this:
from django.db.backends.oracle.base import DatabaseOperations
DatabaseOperations.max_name_length = lambda s: <NEW_MAX_VALUE>
It's not really clear why you need more than 30 characters in table name, as it violates Oracle Schema Object Naming Rules.
精彩评论