How to Fix: PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values
Use all excluded values in PostgreSQL upsert with ON CONFLICT UPDATE
📋 Table of Contents
When using PostgreSQL's INSERT ON CONFLICT UPDATE (upsert) feature, you may have noticed that the syntax can be a bit verbose. The question is, can you use all excluded values in a shorter way?
🔍 Why This Happens
- The reason for this is that the EXCLUDED table contains all columns from the original table, including any unique constraints or indexes. By specifying each column individually in your UPDATE SET clause, you are ensuring that all values are updated correctly.
🚀 How to Resolve This Issue
Method 1: Using EXCLUDED.*
- Step 1: Replace each column name in the UPDATE SET clause with EXCLUDED.column_name, such as id=EXCLUDED.id, username=EXCLUDED.username, etc.
Method 2: Using ALL
- Step 1: Replace the UPDATE SET clause with EXCLUDED.*, which will update all columns from the EXCLUDED table.
🎯 Final Words
By using EXCLUDED.* or ALL, you can simplify your INSERT ON CONFLICT UPDATE (upsert) syntax and make it more readable. However, be aware that using ALL may have performance implications if the table has many columns.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Stuck in tutorial hell after 4 years: How do I b
Fix Stuck in tutorial hell after 4 years: How do I bui. Practice build
How to Fix: Trying to sync mutliple audio tracks to a movie
Fix Trying to sync mutliple audio tracks to a movie bu. Consider using
How to Fix: Failed to merge latest branches from upstream re
Fix Failed to merge latest branches from upstream repo. Try running 'g