gpt4 book ai didi

Django ORM : Get column of DataBaseError

转载 作者:行者123 更新时间:2023-11-29 11:22:27 26 4
gpt4 key购买 nike

有没有办法获取相关的表名和列名,而不仅仅是这条消息?

django.db.utils.DataError: value too long for type character varying(16)

我不喜欢猜测或搜索相关栏目。

我们使用 django 1.6 和 PostgreSQL 9.3.6

完整的回溯

  File "/home/f/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/f/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
cursor.execute(sql, params)
File "/home/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/home/f/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/f/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(16)

最佳答案

没有办法简单地找出来。

Django这里只是引用了DB说的,这是postgres的错:

db=> INSERT INTO db_accesskeys (key, link) VALUES ('AAAAA...', 'BBBBB...');
ERROR: value too long for type character varying(40)

postgresql 不会告诉你哪个太长,所以 django 也无法发现。

解决方法是手动验证每个输入以匹配 max_length(不过我不确定 django 1.6 上的确切实现,这是 1.8):

def validate_length(model, field, value):
if len(value) > model._meta.get_field(field).max_length:
raise Exception(
field + " of " + model._meta.model_name + " is too long (" + str(len(value)) + " > " +
str(model._meta.get_field(field).max_length) + ")"
)


validate_length(AccessKeys, "key", "AAAAAAA...")
Exception: key of accesskeys is too long (109 > 40)

当然,您可以根据需要调整抛出的异常,以便更轻松地访问过长的字段名称,或者查看到底有多少个字符过长或...

关于 Django ORM : Get column of DataBaseError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29227749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com