在约束表中插入记录

时间:2021-01-08 02:00:09

Why do I get an error when trying to add records to a PostGres table, and how to solve this:

尝试将记录添加到PostGres表时,为什么会出现错误,以及如何解决此问题:

INSERT INTO common.levels(level_pid, level_name) VALUES(0,'Level1')

Heres the table structure:

继表结构:

CREATE TABLE IF NOT EXISTS common.levels
(
  level_id SERIAL PRIMARY KEY NOT NULL,
  level_pid INTEGER NOT NULL,
  level_name VARCHAR(40) NOT NULL,
  CONSTRAINT FK_MT_Parent FOREIGN KEY (level_pid) REFERENCES common.levels(level_id) ON DELETE CASCADE
);

Error:

在约束表中插入记录

1 个解决方案

#1


1  

The FK constraint that you have created specifies that the level_pid must already exist as a level_id in this table. Hence, when you are inserting the value 'Level1' this does not exist as a level_id in the table and inserting would therefore violate the FK constraint. I would suggest creating the table, inserting all the required initial data and then inserting the FK constraint afterwards. Note that data must be inserted in a way that eventually each level_pid corresponds to a level_id in the table.

您创建的FK约束指定level_pid必须已作为此表中的level_id存在。因此,当您插入值“Level1”时,这不会作为表中的level_id存在,因此插入会违反FK约束。我建议创建表,插入所有必需的初始数据,然后插入FK约束。请注意,必须以最终每个level_pid对应于表中的level_id的方式插入数据。

Hope this helps.

希望这可以帮助。

#1


1  

The FK constraint that you have created specifies that the level_pid must already exist as a level_id in this table. Hence, when you are inserting the value 'Level1' this does not exist as a level_id in the table and inserting would therefore violate the FK constraint. I would suggest creating the table, inserting all the required initial data and then inserting the FK constraint afterwards. Note that data must be inserted in a way that eventually each level_pid corresponds to a level_id in the table.

您创建的FK约束指定level_pid必须已作为此表中的level_id存在。因此,当您插入值“Level1”时,这不会作为表中的level_id存在,因此插入会违反FK约束。我建议创建表,插入所有必需的初始数据,然后插入FK约束。请注意,必须以最终每个level_pid对应于表中的level_id的方式插入数据。

Hope this helps.

希望这可以帮助。