首页 > 解决方案 > 在 Postgres SQL 中插入期间列“id”中的空值

问题描述

表格消息的结构-

id - Integer - NotNull - Primary Key
message - text - NotNull

当我尝试使用插入时

INSERT INTO messages(message) VALUES ('abc');

它给出了以下错误-

ERROR:  null value in column "id" of relation "messages" violates not-null constraint
DETAIL:  Failing row contains (null, abc).
SQL state: 23502

提前致谢 !

标签: sqlpostgresqlspring-boot

解决方案


将此添加到您的实体中

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

或在 postgres 中将 id 数据类型更改为串行;

id - serial-Primary Key
message - text - NotNull

就像是:

CREATE TABLE post (
    id  SERIAL NOT NULL,
    title VARCHAR(255),
    PRIMARY KEY (id)
)

在串行使用这个(strategy = GenerationType.IDENTITY)


推荐阅读