简体   繁体   English

SERIAL 与 NULL 一起使用,始终作为身份生成而不是

[英]SERIAL works with NULL, GENERATED ALWAYS AS IDENTITY not

Postgres 12: Postgres 12:

CREATE TABLE l_table (
    id INT generated always as identity,
    w_id int NOT null references w_table(id),
    primary key (w_id, id)
)PARTITION BY LIST (w_id);

CREATE table l1 PARTITION OF l_table FOR VALUES IN (1);

insert into l1 (w_id) values (1);

I'm getting:我越来越:

ERROR: null value in column "id" violates not-null constraint

If I replace INT generated always as identity with SERIAL it works .如果我将始终生成的 INT 替换为SERIAL INT generated always as identity ,它就可以工作 This is odd as in another table the generated always as identity works with null.这很奇怪,因为在另一个表中生成的身份总是与 null 一起使用。 Using default as value does not work either.使用default作为值也不起作用。

GAAI is supposed to be the SQL standard way of replacing SERIAL, even It's the suggested one. GAAI应该是SQL替换SERIAL的标准方式,甚至是建议的方式。 What am I missing here?我在这里想念什么?

Thanks.谢谢。

What am I missing here?我在这里想念什么?

You're trying to insert into the partition table l1 directly, instead of the partitioned l_table .您正在尝试直接插入分区表l1 ,而不是分区表l_table This ignores the identity column on the parent table, tries to insert the default null , and fails the non-null constraint that every identity column has.这会忽略父表上的标识列,尝试插入默认的null ,并使每个标识列具有的非空约束失败。 If you instead do如果你改为

insert into l_table (w_id) values (1);

it will work and route the inserted row into the right partition.它将起作用并将插入的行路由到正确的分区中。

Using default as value does not work either.使用default作为值也不起作用。

Apparently it's quite hard to do that.显然,要做到这一点相当困难。 How to DEFAULT Partitioned Identity Column?如何默认分区标识列? over at dba.SE discusses some workarounds.在 dba.SE 上讨论了一些解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最好使用 SERIAL PRIMARY KEY 或 GENERATED ALWAYS AS IDENTITY 作为 PostgreSQL 中的主键 - Better to use SERIAL PRIMARY KEY or GENERATED ALWAYS AS IDENTITY for primary key in PostgreSQL 生成“GENERATED ALWAYS AS IDENTITY”With Spring JPA for PostgreSQL - Generate "GENERATED ALWAYS AS IDENTITY" With Spring JPA for PostgreSQL GENERATED { ALWAYS | 的限制是多少? 默认 } 作为 PostgreSQL 中的身份? - What is the limit of GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY in PostgreSQL? PostgreSQL - 关于 GENERATED ALWAYS AS IDENTITY 的冲突更新 - PostgreSQL - on conflict update for GENERATED ALWAYS AS IDENTITY 在 jpa 中使用 postgres (GENERATED ALWAYS AS IDENTITY) 生成的序列 - Use sequence generated by postgres (GENERATED ALWAYS AS IDENTITY) in jpa pg_restore 如何处理 GENERATED ALWAYS AS IDENTITY 列? - How does pg_restore handle GENERATED ALWAYS AS IDENTITY columns? 我是否需要为“始终以身份生成的 bigint”声明一个唯一约束? - Do I need to declare a unique constraint for `bigint generated always as identity`? PostgreSQL:串行与身份 - PostgreSQL: serial vs identity 如何使用 node-pg-migrate 定义“INT GENERATED ALWAYS AS IDENTITY”列? - How to define "INT GENERATED ALWAYS AS IDENTITY" column with node-pg-migrate? 在Dbeaver上创建Postgres表时,不能使用“ GENERATED ALWAYS AS IDENTITY”吗? - Can't use “GENERATED ALWAYS AS IDENTITY” when creating Postgres tables on Dbeaver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM