无法插入列为外键的 table
Unable to insert into table where column is a foreign key
插入我的 table 时出现此错误:
错误:关系 "Item" 的列 "brandid" 不存在
brandId 列上有一个外键约束,将其链接到另一个 table 的 ID。
我要插入的 table 定义如下:
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 列都是序列类型。
品牌和部门 table 已经填充,那些 select 语句已经过测试并且可以正常工作。
错误告诉您 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'));
插入我的 table 时出现此错误:
错误:关系 "Item" 的列 "brandid" 不存在
brandId 列上有一个外键约束,将其链接到另一个 table 的 ID。
我要插入的 table 定义如下:
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 列都是序列类型。
品牌和部门 table 已经填充,那些 select 语句已经过测试并且可以正常工作。
错误告诉您 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'));