【发布时间】:2019-06-18 00:34:54
【问题描述】:
我在 postgresql 表中有 id 列和 position 列。我想定位自动增量,所以我使用 serial 数据类型添加了 position:
ALTER TABLE tbl ADD COLUMN position serial;
id name position
------- ------- -------
1 S4 4
2 S2 2
3 S3 3
4 S1 1
问题是当我尝试创建新对象时出现以下错误:
org.postgresql.util.PSQLException: error: PSQLException: ERROR: null value in column "position" violates not-null
如何设置position列自动递增?
我的普通对象:
public class Stg {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@NotNull
@Size(max = 20)
@Column(name = "name", length = 20, nullable = false)
private String name;
private Integer position;
【问题讨论】:
-
你的 Postgres 版本是什么?
-
@TimBiegeleisen 10.1
-
你的 INSERT 语句是什么?
-
您的 alter 语句本身不应导致此错误 AFAIK。
-
@404 我放入语句中的值看起来像 (5, S5, null) 所以位置显然是 null 但是我没有 id 的问题 - 它是生成的
标签: java postgresql auto-increment