Django 错误 ORA-01461 解决方法? [英] Django error ORA-01461 workaround?

查看:46
本文介绍了Django 错误 ORA-01461 解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Django(1.5 版)应用程序出现此 Oracle 错误:

I have a Django (version 1.5) application that is getting this Oracle error:

ORA-01461: can bind a LONG value only for insert into a LONG column

经过一些数据库调试后,问题似乎是由于在 DJANGO_SESSION 表上执行 INSERT 导致的,其中 SESSION_DATA 的 Unicode 字符串值很长(大约 2.5k 个字符)(数据类型为 NCLOB).

After doing some database debugging, it appears that the problem is caused by doing an INSERT on the DJANGO_SESSION table with a long (roughly 2.5k characters) Unicode string value for SESSION_DATA (data type NCLOB).

我的一位同事告诉我,该问题不会发生在 UPDATE 上,而仅发生在 INSERT 上,因为 UPDATE 的 Django 数据库代码包含将数据库写入拆分为可管理块的逻辑(我在这里解释一下),但是对于INSERT 缺少逻辑的某些原因.

I have been told by a coworker that the problem does not occur on UPDATEs but only on INSERTs, because the Django database code for UPDATEs contains logic to (I'm paraphrasing here) split the database write into manageable chunks, but for some reason that logic is missing for INSERTs.

有了这些信息,我想我可以通过编写一小块自定义中间件来解决它,该中间件立即执行 request.session.save() 并为 SESSION_DATA 设置一个(大概)空值,然后在会话时执行数据已设置,这将导致更新而不是插入.

Armed with this information, I figured I could work around it by writing a small piece of custom middleware that does an immediate request.session.save() with a (presumably) empty value for SESSION_DATA and then later on when the session data is set, that would cause an UPDATE instead of an INSERT.

然而,事情似乎并没有那么简单.如果我在django.contrib.sessions.middleware.SessionMiddleware"条目上方的 settings.MIDDLEWARE_CLASSES 中插入我的自定义中间件,请求对象还没有会话属性,并且我收到此错误:

However, it appears not to be that simple. If I insert my custom middleware in settings.MIDDLEWARE_CLASSES above the entry for 'django.contrib.sessions.middleware.SessionMiddleware', the request object doesn't have the session attribute yet, and I get this error:

AttributeError: 'WSGIRequest' object has no attribute 'session'

如果我在 SessionMiddleware 条目之后插入我的中间件,则过程为时已晚,我收到原始 ORA-01461 错误.

And if I insert my middleware after the SessionMiddleware entry, it's too late in the process and I get the original ORA-01461 error.

那么,有没有办法使用中间件来解决 ORA-01461 错误?或者根本没有?

So, is there a way to work around the ORA-01461 error using middleware? Or at all?

推荐答案

对我有用的是在会话数据变得非常大之前创建和保存会话对象:

what worked for me was creating and saving a session object before the session data gets very big:

from django.contrib.sessions.backends.db import SessionStore
def view(request): 
    s = SessionStore()
    s.create()
    request.session = s
    s.save()
    # rest of the code here

后续查询作为更新命令执行,而不是作为 oracle 插入.

subsequent queries are executed as update commands and not as inserts by oracle.

这篇关于Django 错误 ORA-01461 解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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