Cannot insert data, foreign key error on postgresql












1















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









share|improve this question





























    1















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









    share|improve this question



























      1












      1








      1








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









      share|improve this question
















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






      sql postgresql sql-insert






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 7:16









      a_horse_with_no_name

      296k46451546




      296k46451546










      asked Nov 19 '18 at 19:46









      AmorosoAmoroso

      1571212




      1571212
























          3 Answers
          3






          active

          oldest

          votes


















          0














          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






          share|improve this answer































            0














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






            share|improve this answer































              0














              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






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381631%2fcannot-insert-data-foreign-key-error-on-postgresql%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                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






                share|improve this answer




























                  0














                  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






                  share|improve this answer


























                    0












                    0








                    0







                    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






                    share|improve this answer













                    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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 '18 at 21:20









                    Nick EllisNick Ellis

                    699718




                    699718

























                        0














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






                        share|improve this answer




























                          0














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






                          share|improve this answer


























                            0












                            0








                            0







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






                            share|improve this answer













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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 2:23









                            dwir182dwir182

                            1,418618




                            1,418618























                                0














                                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






                                share|improve this answer




























                                  0














                                  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






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    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






                                    share|improve this answer













                                    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







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 21 '18 at 7:30









                                    a_horse_with_no_namea_horse_with_no_name

                                    296k46451546




                                    296k46451546






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381631%2fcannot-insert-data-foreign-key-error-on-postgresql%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                        ComboBox Display Member on multiple fields

                                        Is it possible to collect Nectar points via Trainline?