简体   繁体   English

无法插入数据,PostgreSQL上的外键错误

[英]Cannot insert data, foreign key error on postgresql

I have 3 tables, where the table INTRUSIONS have foreign keys belonging to CCTVS and ALARMS tables. 我有3个表,其中表INTRUSIONS具有属于CCTVS和ALARMS表的外键。 I want the foreign keys to be nullable in my INTRUSIONS table. 我希望外键在我的INTRUSIONS表中可以为空。 I am not sure why but I cannot insert data into my 'INTRUSIONS' table. 我不确定为什么,但是无法将数据插入“ INTRUSIONS”表中。 Here is my code: 这是我的代码:

CREATE  TABLE
REMOTE_SECURITY.CCTVS(CCTV_ID   serial);

CREATE  TABLE
REMOTE_SECURITY.ALARMS(ALARM_ID serial);

CREATE  TABLE
REMOTE_SECURITY.INTRUSIONS(INTRUSION_ID serial,CCTV_ID  serial,ALARM_ID serial);

ALTER   TABLE
REMOTE_SECURITY.CCTVS   ADD CONSTRAINT
CCTVS_PK    PRIMARY KEY
(CCTV_ID)
;
ALTER   TABLE
REMOTE_SECURITY.ALARMS  ADD CONSTRAINT
ALARMS_PK   PRIMARY KEY
(ALARM_ID)
;

This code doesn't work: 该代码不起作用:

  INSERT
    INTO
        REMOTE_SECURITY.INTRUSIONS
    (
        INTRUSION_ID
    ,   CCTV_ID
    ,   ALARM_ID
    ) VALUES (
        1
    ,   NULL
    ,   1
    )
    ;

Here is the error: 这是错误:

[2018-11-19 19:35:59] [23502] ERROR: null value in column "cctv_id" violates not-null constraint
[2018-11-19 19:35:59] Detail: Failing row contains (1, null, 1, 2010-02-01 07:00:01).

The foreign key forces the column to be non null. 外键强制该列为非null。 If you want a foreign key association it won't know how to relate null to another table. 如果您想要一个外键关联,它将不知道如何将null与另一个表关联。 From the documentation 从文档中

A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. 外键约束指定一列(或一组列)中的值必须与另一个表的某一行中出现的值匹配。 We say this maintains the referential integrity between two related tables. 我们说这保持了两个相关表之间的参照完整性。

https://www.postgresql.org/docs/9.4/ddl-constraints.html https://www.postgresql.org/docs/9.4/ddl-constraints.html

Your Error are clear.. You cannot put Null value to Foreign Key or Serial .. Maybe you are wrong in the concept design your schema .. 您的Error很明显..您不能将Null值设置为Foreign KeySerial ..也许您在概念设计schema是错误的..

CREATE TABLE tablename (
    colname SERIAL
);

is equivalent to specifying: 等效于指定:

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

As noted by Documentation Serial data type hold Not Null so it can't be Null and Other answer really great tell about constraint foreign key.. Documentation Serial数据类型保持Not Null因此不能为Null ,其他答案确实很好地说明了约束外键。

As Documentation FK said 正如Documentation FK所说

We say this maintains the referential integrity between two related tables 我们说这保持了两个相关表之间的参照完整性

So.. The value in FK are referencing to Parents value which it is the PK .. PK cannot have Null so FK too.. 所以.. FK中的值是指作为PK父级值PK也不能为Null所以FK也是如此。

A foreign key column ( intrusions.cctv_id or intrusions.alarm_id in your case) should not be defined as serial as you don't want to generate new values every time you insert into them. 外键列( intrusions.cctv_idintrusions.alarm_id你的情况) 应该被定义为serial ,你不希望产生新的价值每次插入他们的时间。 You use the generated values from the referenced primary key column(s) 您使用从引用的主键列生成的值

As dwir182 has pointed out, a serial column is implicitly defined as not null . 如dwir182所指出的, serial列被隐式定义为not null By fixxing the incorrect definition of the FK columns, you will be able to insert NULL values. 通过修正错误的FK列定义,您将能够插入NULL值。

So you want: 所以你要:

CREATE TABLE remote_security.intrusions
(
  intrusion_id   serial,
  -- no serial for the foreign key columns!
  cctv_id        integer references cctvs,
  alarm_id       integer references alarms
);

Then you can do the following: 然后,您可以执行以下操作:

insert into remote_security.cctvs values (default); -- creates id = 1;
insert into remote_security.alarms values (default); -- creates id = 1;

-- do not specify a value for the serial column!
insert into remote_security.intrusions 
  (intrusion_id, cctv_id, alarm_id)
values 
  (default, null, 1);

See the online example: https://rextester.com/ELPHK12991 参见在线示例: https : //rextester.com/ELPHK12991

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM