SQL 奇怪的PostgreSQL“值太长,类型字符变化(500)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13485030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strange PostgreSQL "value too long for type character varying(500)"
提问by Parham
I have a Postgres schema which looks like:
我有一个 Postgres 架构,它看起来像:
The problem is that whenever I save text longer than 500 characters in the description column I get the error:
问题是,每当我在描述列中保存超过 500 个字符的文本时,我都会收到错误消息:
value too long for type character varying(500)
In the documentation for Postgres it says type text can have unlimited characters.
在 Postgres 的文档中,它说类型文本可以有无限的字符。
I'm using postgresql-9.1.
我正在使用 postgresql-9.1。
This table has been generated using Django 1.4 and the field type in the model is TextField, if that helps explain the problem further.
该表是使用 Django 1.4 生成的,模型中的字段类型是 TextField,如果这有助于进一步解释问题。
Any ideas as why this is happening and what I can do to fix it?
关于为什么会发生这种情况以及我可以做些什么来解决它的任何想法?
采纳答案by Craig Ringer
By specifying the column as VARCHAR(500)
you've set an explicit 500 character limit. You might not have done this yourself explicitly, but Django has done it for you somewhere. Telling you where is hard when you haven't shown your model, the full error text, or the query that produced the error.
通过指定列,因为VARCHAR(500)
您已经设置了明确的 500 个字符限制。您可能没有明确地自己完成此操作,但 Django 已在某处为您完成。当你没有显示你的模型、完整的错误文本或产生错误的查询时,告诉你哪里很难。
If you don't want one, use an unqualified VARCHAR
, or use the TEXT
type.
如果您不需要,请使用不合格的VARCHAR
,或使用TEXT
类型。
varchar
and text
are limited in length only by the system limits on column size - about 1GB - and by your memory. However, adding a length-qualifier to varchar
sets a smaller limit manually. All of the following are largely equivalent:
varchar
并且text
长度仅受系统对列大小的限制 - 大约 1GB - 和您的内存。但是,添加长度限定符以varchar
手动设置较小的限制。以下所有内容在很大程度上是等效的:
column_name VARCHAR(500)
column_name VARCHAR CHECK (length(column_name) <= 500)
column_name TEXT CHECK (length(column_name) <= 500)
The only differences are in how database metadata is reported and which SQLSTATE is raised when the constraint is violated.
唯一的区别在于如何报告数据库元数据以及在违反约束时引发哪个 SQLSTATE。
The length constraint is not generally obeyed in prepared statement parameters, function calls, etc, as shown:
在准备好的语句参数、函数调用等中一般不遵守长度约束,如下所示:
regress=> \x
Expanded display is on.
regress=> PREPARE t2(varchar(500)) AS SELECT ;
PREPARE
regress=> EXECUTE t2( repeat('x',601) );
-[ RECORD 1 ]-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
?column? | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
and in explicit casts it result in truncation:
并在显式转换中导致截断:
regress=> SELECT repeat('x',501)::varchar(1);
-[ RECORD 1 ]
repeat | x
so I think you areusing a VARCHAR(500)
column, and you're looking at the wrong table or wrong instance of the database.
所以我认为您正在使用一VARCHAR(500)
列,并且您正在查看错误的表或数据库的错误实例。
回答by Scott S
Character varying is different than text. Try running
字符变化与文本不同。尝试跑步
ALTER TABLE product_product ALTER COLUMN code TYPE text;
That will change the column type to text, which is limited to some very large amount of data (you would probably never actually hit it.)
这会将列类型更改为文本,这仅限于一些非常大量的数据(您可能永远不会真正点击它。)
回答by Marco Lucio Lorenzotti
We had this same issue. We solved it adding 'length' to entity attribute definition:
我们有同样的问题。我们解决了在实体属性定义中添加“长度”的问题:
@Column(columnDefinition="text", length=10485760)
private String configFileXml = "";