Postgres upsert with ON CONFLICT

@linuxgnuยท2d ago156 views
SQLPublic
-- Insert a row, or update it if the unique key already exists
INSERT INTO settings (user_id, key, value)
VALUES ($1, $2, $3)
ON CONFLICT (user_id, key)
DO UPDATE SET value = EXCLUDED.value, updated_at = now()
RETURNING *;

Comments(0)

No comments yet. Start the conversation.