最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is ON CONFLICT DO UPDATE not working with PostgreSQL column aliasing? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to perform an INSERT operation with an ON CONFLICT DO UPDATE clause in PostgreSQL. My table contains column names with aliases (e.g., "ITEM_CODE(material_code)"), and I want to update the corresponding fields when a conflict occurs on "ITEM_CODE(material_code)". Here's the SQL query I'm using:

INSERT INTO api_itemmaster (
    "ITEM_CODE(material_code)",
    "ITEM_NAME(material_name)",
    "ITEM_GROUP_CODE",
    "UNIT(base_unit)",
    "PACK(pack_size)",
    "ITEM_CLASS_CODE(material_type)",
    "DIVISION_CODE(division)",
    "SEGMENT_CODE",
    "CAT_CODE",
    "ACTIVE",
    "FORMT",
    created_on,
    updated_on,
    active_state
) VALUES (
    '5000',
    'OSKAR',
    '225',
    'STR',
    '15',
    'ZF',
    '8000',
    '30000',
    '004',
    '1',
    '20',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP,
    false
) ON CONFLICT ("ITEM_CODE(material_code)") 
DO UPDATE SET
    "ITEM_NAME(material_name)" = EXCLUDED."ITEM_NAME(material_name)",
    "ITEM_GROUP_CODE" = EXCLUDED."ITEM_GROUP_CODE",
    "UNIT(base_unit)" = EXCLUDED."UNIT(base_unit)",
    "PACK(pack_size)" = EXCLUDED."PACK(pack_size)",
    "ITEM_CLASS_CODE(material_type)" = EXCLUDED."ITEM_CLASS_CODE(material_type)",
    "DIVISION_CODE(division)" = EXCLUDED."DIVISION_CODE(division)",
    "SEGMENT_CODE" = EXCLUDED."SEGMENT_CODE",
    "CAT_CODE" = EXCLUDED."CAT_CODE",
    "ACTIVE" = EXCLUDED."ACTIVE",
    "FORMT" = EXCLUDED."FORMT",
    updated_on = EXCLUDED.updated_on;

However, the ON CONFLICT DO UPDATE clause doesn't seem to work, and no updates are applied to the existing row.

Ensured that the table and column names are correct. Verified that the ITEM_CODE(material_code) constraint is unique in the table. Confirmed that the syntax aligns with PostgreSQL's ON CONFLICT documentation.

发布评论

评论列表(0)

  1. 暂无评论