insert into hospital_image
select 'HospitalImage',
lo_from_bytea(1,decode('/9j/4AAQSkZJRgABAQEA3ADcAAD','base64')),
'jpg',
'123'
where not exists (select null from pg_largeObject where loid=1);
CREATE TABLE hospital_image (
key character varying(30) NOT NULL,
image oid NOT NULL,
mime_type character varying(30) NOT NULL,
version numeric(8,0) NOT NULL,
CONSTRAINT
pk_hospital_image PRIMARY KEY (key)
) WITH ( OIDS=FALSE );
ALTER TABLE
hospital_image OWNER TO postgres;
在上面的语句中,我们将loid
手动提供为 1。相反,我们希望loid
动态使用lo_create(0)
. 当我lo_create(0)
按照Postgres docs使用时,我得到一个异常。
我同时使用了lo_creat(-1)
和lo_create(0)
。两者都不起作用。就是说loid
已经存在了。如何在我的查询中使用上述功能。
包含变量 OID 的我的 SQL 语句是:
INSERT INTO hospital_image (key, image, mime_type, version)
VALUES ('MainLogoImage99999',
lo_from_bytea(lo_create(0),
decode('/9j4AAQSkZJRgABAQEA3ADcAAD',
'base64')),
'jpg',
123);
错误信息是:
ERROR: duplicate key value violates unique constraint "pg_largeobject_metadata_oid_index"
SQL state: 23505
Detail: Key (oid)=(34773) already exists.