sql - 在 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
提前致谢 !
解决方案
将此添加到您的实体中
@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)
推荐阅读
- swift - 如何自动更新 Firestore 中更改的信息
- webforms - 如何在 XLSForm 中创建弹出窗口?
- python - Python 中 exec() 的问题
- r - 优化 R 中的逐行矩阵比较
- pandas - 代码显示错误:nan,它与 np.nan 有关还是其他错误?
- javascript - 我需要在翻转时钟倒计时中添加一个活动类
- node.js - 使用护照和快速会话创建用户帐户登录页面
- webrtc - 如何在 Ant Media Server 中设置 SameSite 属性?
- javascript - 我正在尝试为音轨的左右声道设置增益,以便我可以通过两个扬声器一次播放任一声道
- php - 当我在本地主机上激活 asta 主题时出现致命错误