简体   繁体   中英

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. I want the foreign keys to be nullable in my INTRUSIONS table. I am not sure why but I cannot insert data into my 'INTRUSIONS' table. 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. If you want a foreign key association it won't know how to relate null to another table. 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

Your Error are clear.. You cannot put Null value to Foreign Key or Serial .. Maybe you are wrong in the concept design your 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..

As Documentation FK said

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..

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. 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 . By fixxing the incorrect definition of the FK columns, you will be able to insert NULL values.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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