PostgreSQL插入语句报错:布尔类型输入语法无效原因咨询
PostgreSQL INSERT语句布尔类型语法错误原因分析
执行的INSERT语句:
INSERT INTO "attack_paths_attackpathprevscan" ("createdAt","account_id","org_id","event_id","action_id","assets") VALUES (('2022-10-30 08:51:13.934641')::timestamp, ('cd802832-c0e8-4376-a1f7-730836cb0885'::uuid)::uuid, ('e5f8eaff-54f0-4ea0-8428-b331a504d744'::uuid)::uuid, ('11111111-1111-1111-1111-111111111119')::uuid, ('11111111-1111-1111-1111-111111111119')::uuid, (ARRAY[hstore(ARRAY['id','type','asset_id','group_id','is_internet_facing','is_running','connecting_agent_id','policies'], ARRAY['ap1-node1','VmNodeData','11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111112',true,true,'ap1-node1-al',NULL])])::jsonb)
报错信息:
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type boolean: "ap1-node1"
LINE 3: ...running','connecting_alert_id','policies'], ARRAY['ap1-node1...
错误原因:
问题出在构建hstore时使用的第二个数组——你混合了字符串、布尔字面量true和NULL。PostgreSQL处理数组时,会自动将数组内所有元素转换为统一类型,而布尔类型的优先级高于字符串,因此数据库会尝试把数组里的所有元素都转成布尔类型。但第一个元素'ap1-node1'是普通字符串,无法被解析为有效的布尔值(PostgreSQL仅认可true/false、t/f等有限的布尔格式),于是触发了这个语法错误。
另外要注意,hstore的键值对本质都是字符串类型,不需要直接传入布尔字面量,转成jsonb时字符串形式的'true'会被自动识别为布尔类型。
修复方案:
把数组里的true改成字符串形式'true',确保第二个数组的所有元素都是字符串类型:
ARRAY['ap1-node1','VmNodeData','11111111-1111-1111-1111-111111111111','11111111-1111-1111-1111-111111111112','true','true','ap1-node1-al',NULL]
内容的提问来源于stack exchange,提问作者CrazySynthax
相关产品推荐
相关产品推荐

