Strange PostgreSQL “value too long for type character varying(500)”

后端 未结 3 810
旧时难觅i
旧时难觅i 2021-01-31 01:27

I have a Postgres schema which looks like:

\"enter

The problem is that whenever I

相关标签:
3条回答
  • 2021-01-31 02:02

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

    0 讨论(0)
  • 2021-01-31 02:14

    We had this same issue. We solved it adding 'length' to entity attribute definition:

    @Column(columnDefinition="text", length=10485760)
    private String configFileXml = ""; 
    
    0 讨论(0)
  • 2021-01-31 02:19

    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.

    If you don't want one, use an unqualified VARCHAR, or use the TEXT type.

    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:

    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.

    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 $1;
    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 are using a VARCHAR(500) column, and you're looking at the wrong table or wrong instance of the database.

    0 讨论(0)
提交回复
热议问题