42

I have the following command in postgresql:

INSERT INTO word_relations(word1_id, word2_id, score) VALUES($1, $2, $3)
ON CONFLICT (word1_id, word2_id) DO UPDATE SET score = score + $3`)

I get the following error:

column reference "score" is ambiguous

I thought it was odd as I am only using one table. Any ideas?

1 Answer 1

74

On the right side of the = in the set clause, there are two possibilities for score: EXCLUDED.score and word_relations.score. The former is a way of accessing the value being inserted; the latter a way of accessing the value stored in the row.

I would write this as:

ON CONFLICT (word1_id, word2_id) DO
    UPDATE SET score = word_relations.score + EXCLUDED.score
2
  • In which case will word_relations.score be different from EXCLUDED.score? Is there a reason for the split between them when its clearly stated that the conflict is on that column ??
    – leoOrion
    Jan 25, 2017 at 10:44
  • 5
    word_relations.score is the old value, and EXCLUDED.score is the new one you're inserting now Apr 7, 2017 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.