如果存在外键,则插入PostgreSQL

Jen*_*ijn 7 postgresql

仅当外键(在本例中为模型)存在时,如何在具有外键引用的表中插入新行?

目前我有以下声明:

INSERT INTO furniture (model, type) VALUES (modelA, chair)
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 8

如果FK不存在,请使用不返回任何内容的SELECT.

INSERT INTO furniture (model, type) 
select 'modelA', 'chair'
where exists (select * 
              from model 
              where model.model = 'modelA');
Run Code Online (Sandbox Code Playgroud)

您没有告诉我们调用引用的表是什么.我认为是model- 你需要调整到真实姓名.