【问题标题】:ERROR: value too long for type character varying(255) in postgresql错误:postgresql 中类型字符变化(255)的值太长
【发布时间】:2022-01-13 10:17:51
【问题描述】:

我正在使用 spring-boot 和 JPA 这是我用于列表的字段

@ElementCollection
@Column(columnDefinition="TEXT")
@CollectionTable(name="general_values")
private List<String> values;

但我收到错误

Caused by: org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255)
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar:42.2.5]

但是在value 的数据库表类型中显示为文本。由于我使用TEXT 作为类型并且在表格列中也显示为text,但错误仍然是varying(255).

以及值字段的请求负载

"values":["45","There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable"]

【问题讨论】:

    标签: java spring spring-boot spring-data-jpa


    【解决方案1】:

    默认情况下,您可以存储的最大字符数为 255。试试这个

    @ElementCollection
    @Column(columnDefinition="TEXT", length = 2048)
    @CollectionTable(name="general_values")
    private List<String> values;
    

    【讨论】:

    • 唱得到同样的异常
    • 你能检查一下数据库中的列属性,看看那里的长度是多少。是 255 还是 2048 ?
    【解决方案2】:

    首先你需要删除表格,然后在列中添加长度属性,只有你才能看到效果。

    @ElementCollection
    @Column(columnDefinition="TEXT", length = 1000)
    @CollectionTable(name="general_values")
    private List<String> values;
    

    否则你可以使用SQL命令修改长度

    ALTER TABLE table_name MODIFY column_name varchar(new_length);  
    

    【讨论】:

      【解决方案3】:

      要解决此错误,您必须在 @Column 包含长度属性的位置再次创建架构,或者您可以手动更新数据库中的表。

      @Column(columnDefinition="TEXT", length = 1000)
      private List<String> values;
      

      生成模式时,它将创建一个大小为 1000 个字符的列。对现有表没有影响。

      【讨论】:

        猜你喜欢
        • 2015-09-26
        • 2021-06-22
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 2019-10-01
        • 2021-10-20
        相关资源
        最近更新 更多