无法插入表中列为外键的地方

时间:2018-09-11 03:13:52

标签: sql postgresql quoted-identifier

插入表中时出现此错误:

错误:关系“项目”的列“ brandid”不存在

brandId列具有外键约束,可将其链接到另一个表的ID。

我要插入的表的定义如下:

Column  |  Type   |                      Modifiers                      | Storage  | Stats target | Description 
---------+---------+-----------------------------------------------------+----------+--------------+-------------
 id      | integer | not null default nextval('"Item_id_seq"'::regclass) | plain    |              | 
 name    | text    | not null                                            | extended |              | 
 price   | money   | not null                                            | plain    |              | 
 sizes   | json    | not null                                            | extended |              | 
 brandId | integer | not null                                            | plain    |              | 
 deptId  | integer | not null                                            | plain    |              | 
Indexes:
    "item_pk" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "Item_fk0" FOREIGN KEY ("brandId") REFERENCES "Brand"(id)
    "Item_fk1" FOREIGN KEY ("deptId") REFERENCES "Department"(id)

我正在尝试执行以下插入语句:

INSERT INTO "Item" (name, price, sizes, brandId, deptId) VALUES
        ('Air Force 1', '120.00', '{"12" : 1 , "10" : 12}',
            (SELECT id FROM "Brand" WHERE name= 'Nike'),
            (SELECT id FROM "Department" WHERE name= 'Mens Shoes'));

我数据库中的所有id列均为serial类型。

“品牌”和“部门”表已被填充,并且这些选择语句已经过测试并可以正常工作。

1 个答案:

答案 0 :(得分:0)

错误告诉您pgsql无法找到字段brandid(而不是您期望的brandId)。区别在于我与我。 尝试在插入查询中将字段名称放在双引号

INSERT INTO "Item" (name, price, sizes, "brandId", "deptId") VALUES
    ('Air Force 1', '120.00', '{"12" : 1 , "10" : 12}',
        (SELECT id FROM "Brand" WHERE name= 'Nike'),
        (SELECT id FROM "Department" WHERE name= 'Mens Shoes'));