代码之家  ›  专栏  ›  技术社区  ›  ylmzzsy

springboot查找要插入postgres数据库的错误序列

  •  0
  • ylmzzsy  · 技术社区  · 2 年前

    我有一个实体;

    @Entity
    @Table(name = "organization")
    public class Organization {
    
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private long id;
    
      @Column(name = "name")
      private String name;
    .
    .
    .
    

    我创建了表格:

    create table organization (
        id int generated always as identity primary key,
        name text unique not null
    );
    

    作为该查询的结果, organization_id_seq 在数据库中创建。

    当我发送请求并尝试插入数据时,它失败并显示错误消息

    jdbc.spi.SqlExceptionHelper: ERROR: relation "organization_seq" does not exist
    

    organization_seq 真的不存在但是 组织_id_seq 存在,我该如何修复?

    我试过了 GenerationType.AUTO 结果是同样的错误。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Jens Schauder    2 年前

    设置 GeneratedValue.strategy GenerationType.IDENTITY

    当表具有PK的标识列时,这是正确的策略。

    什么 AUTO 取决于PK和数据库的数据类型,在您的情况下,似乎最终尝试使用序列。 如果要使用序列,请在架构创建脚本中创建一个具有正确名称的序列,并删除id列的标识声明。

    请参见示例 https://www.baeldung.com/hibernate-identifiers 了解有关如何生成id的更多信息。