可以阻止Django截断长表名吗?

编程入门 行业动态 更新时间:2024-10-28 21:17:06
本文介绍了可以阻止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?

推荐答案

截断由 quote_name 方法,该方法遵循SQL92要求,并使用硬编码值 max_name_length .

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个字符,因为它违反了 Oracle架构对象命名规则.

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

更多推荐

可以阻止Django截断长表名吗?

本文发布于:2023-07-22 03:11:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   长表名吗

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!