尝试使用暂存 table 中的标识列插入 table 时出错
Errors attempting to INSERT INTO table with identity column from a staging table
我正在尝试将暂存 table 中的数据插入到另一个带有标识列的类似 table 中,但无法使 SQL 语法正确无误。这是在 PostgreSQL 14.
分期table:
CREATE TABLE IF NOT EXISTS public.productstaging
(
guid varchar(64) NOT NULL,
productimagehash_sha2256 varchar(64) NOT NULL,
productimage Bytea NOT NULL,
UNIQUE (productimagehash_sha2256)
);
Table 插入:
CREATE TABLE IF NOT EXISTS public.product
(
id int NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
guid varchar(64) NOT NULL,
productimagehash_sha2256 varchar(64) NOT NULL,
productimage Bytea NOT NULL
);
插入查询:
-- Insert
INSERT INTO public.product
SELECT
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
FROM public.productstaging
LEFT OUTER JOIN public.product
ON (
public.product.guid = public.productstaging.guid
AND public.product.productimagehash_sha2256 = public.productstaging.productimagehash_sha2256
)
WHERE public.product.guid IS NULL
AND public.product.productimagehash_sha2256 IS NULL;
我收到一个错误
ERROR: column "id" is of type integer but expression is of type character varying
我在查询中尝试了几种方法(如下所列),但它们都出错了。大多数示例在搜索时从固定值列表插入而不是从另一个 table 插入,例如 ...VALUES(guid, productimagehash_sha2256, productimage)...
。我在搜索中找不到类似的东西,希望有人能给我指明正确的方向吗?
...
DEFAULT, --ERROR: DEFAULT is not allowed in this context
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
...
0, --ERROR: cannot insert a non-DEFAULT value into column "id"
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
...
null, --ERROR: cannot insert a non-DEFAULT value into column "id"
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
指定 INSERT 的目标列 - 您应该经常做的事情。
INSERT INTO public.product (guid, productimagehash_sha2256, productimage )
SELECT productstaging.guid,
productstaging.productimagehash_sha2256,
productstaging.productimage
FROM public.productstaging
LEFT JOIN ...
显然您将 guid, productimagehash_sha2256
的组合视为唯一。如果您在这些列上创建唯一索引:
create unique index on productstaging (guid, productimagehash_sha2256);
然后您的 INSERT 语句变得更加简单:
INSERT INTO public.product (guid, productimagehash_sha2256, productimage )
SELECT guid,
productimagehash_sha2256,
productimage
FROM public.productstaging
ON CONFLICT (guid, productimagehash_sha2256)
DO NOTHING;
请注意,如果 guid
存储了真实的 UUID,则该列的定义类型应为 uuid
而不是 varchar
我正在尝试将暂存 table 中的数据插入到另一个带有标识列的类似 table 中,但无法使 SQL 语法正确无误。这是在 PostgreSQL 14.
分期table:
CREATE TABLE IF NOT EXISTS public.productstaging
(
guid varchar(64) NOT NULL,
productimagehash_sha2256 varchar(64) NOT NULL,
productimage Bytea NOT NULL,
UNIQUE (productimagehash_sha2256)
);
Table 插入:
CREATE TABLE IF NOT EXISTS public.product
(
id int NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
guid varchar(64) NOT NULL,
productimagehash_sha2256 varchar(64) NOT NULL,
productimage Bytea NOT NULL
);
插入查询:
-- Insert
INSERT INTO public.product
SELECT
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
FROM public.productstaging
LEFT OUTER JOIN public.product
ON (
public.product.guid = public.productstaging.guid
AND public.product.productimagehash_sha2256 = public.productstaging.productimagehash_sha2256
)
WHERE public.product.guid IS NULL
AND public.product.productimagehash_sha2256 IS NULL;
我收到一个错误
ERROR: column "id" is of type integer but expression is of type character varying
我在查询中尝试了几种方法(如下所列),但它们都出错了。大多数示例在搜索时从固定值列表插入而不是从另一个 table 插入,例如 ...VALUES(guid, productimagehash_sha2256, productimage)...
。我在搜索中找不到类似的东西,希望有人能给我指明正确的方向吗?
...
DEFAULT, --ERROR: DEFAULT is not allowed in this context
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
...
0, --ERROR: cannot insert a non-DEFAULT value into column "id"
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
...
null, --ERROR: cannot insert a non-DEFAULT value into column "id"
public.productstaging.guid,
public.productstaging.productimagehash_sha2256,
public.productstaging.productimage
...
指定 INSERT 的目标列 - 您应该经常做的事情。
INSERT INTO public.product (guid, productimagehash_sha2256, productimage )
SELECT productstaging.guid,
productstaging.productimagehash_sha2256,
productstaging.productimage
FROM public.productstaging
LEFT JOIN ...
显然您将 guid, productimagehash_sha2256
的组合视为唯一。如果您在这些列上创建唯一索引:
create unique index on productstaging (guid, productimagehash_sha2256);
然后您的 INSERT 语句变得更加简单:
INSERT INTO public.product (guid, productimagehash_sha2256, productimage )
SELECT guid,
productimagehash_sha2256,
productimage
FROM public.productstaging
ON CONFLICT (guid, productimagehash_sha2256)
DO NOTHING;
请注意,如果 guid
存储了真实的 UUID,则该列的定义类型应为 uuid
而不是 varchar