可以阻止Django截断长表名吗? [英] Can Django be prevented from truncating long table names?

查看:45
本文介绍了可以阻止Django截断长表名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Django与现有的Oracle DB配合使用(即Django未创建表的数据库).因此,在我的模型中,我必须通过在Meta类中为db_table指定一个值来指示表名.我遇到了问题,因为我希望访问的表与我拥有凭据的用户属于不同的用户.我有权查看表(在SQL Developer中这样做很容易).

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).

如果Oracle表的名称原本超过30个字节,则Django会砍掉该名称的最后四个字节,并用表名其余部分的可重复的4字节哈希替换它们.这对于Django自己制作的表来说都是一件好事.通常,访问现有数据库中的表也不会出现问题(例如我的例子),因为Oracle本身会将名称限制为30个字节.

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 the names to 30 bytes.

问题是Django没有单独的功能来指出该表属于另一个用户.因此,我使用的是点语法解决方法(只需将db_table设置为例如"USERNAME.MY_29_BYTE_TABLE_NAMEXXXXXXXX"),但是由于这会导致整体表名超过30个字节,因此Django会执行其截断技巧并尝试查询不存在的表名.

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.

您可以通过创建自定义数据库后端或通过Monkeypatch这样的方式来覆盖此行为:

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>

尚不清楚为什么表名需要超过30个字符,因为它违反了

It's not really clear why you need more than 30 characters in table name, as it violates Oracle Schema Object Naming Rules.

这篇关于可以阻止Django截断长表名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆