1

插入我的表时,我收到此错误:

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

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 列都是串行类型。

Brand 和 Department 表已经被填充,并且这些 select 语句已经过测试并且可以正常工作。

4

1 回答 1

0

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

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'));
于 2018-09-11T03:20:01.710 回答