1

My understanding of ambiguous fields, or where I see the error elsewhere, is when I've defined a JOIN on multiple records and failed to specifically say which table has the field when the field names are identical.

What I have is (generalized):

INSERT INTO schema_a.table_a(f1,f2,f3,f4,f5,f6)
VALUES(v1,v2,v3,v4,v5,v6)
ON CONFLICT(f1,f2)
DO UPDATE SET (f6) = (f6 + 1)
WHERE schema_a.table_a.f1 = v1 and schema_a.table_a.f2 = v2;

During an update there isn't anything that actually needs to be updated, or is new. I'm preventing PK failures during import by doing this, which should be harmless overall. f6 is just an small integer that gets incremented whenever the record is added again.

PostgreSQL complains that the column f6 is ambiguous, yet there is only 1 table, and exactly one field named f6.

column reference "f6" is ambiguous

Modifying the UPDATE portion by adding the schema and/or table (schema_a.table_a.f6 +1) results in:

column reference "f1" is ambiguous

I can't understand how any part of it, is at all is ambiguous. There isn't a 2nd field anywhere that is identical that the SQL is "touching", and there is only ONE table.

Any help is appreciated.

* Update *

There was a claim I wasn't giving all the information. The following SQL runs inside a trigger function. You will notice the comment tag. The SQL is working as long as the comment tags and the ';' after values are gone. This SQL is exactly exactly the same with the exception that schema, table, field, and value names have been changed with regex replace statements.

INSERT INTO schema_a.table_a(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11)
VALUES(wrk_f1,wrk_f2,wrk_f3,wrk_f4,wrk_f5,wrk_f6,wrk_f7,wrk_f8,wrk_f9,wrk_f10,0);
/*ON CONFLICT (f1,f7)
DO UPDATE SET (f11) = (f11 + 1)
WHERE f1 = wrk_f1 AND f7 = NEW.f7 AND f4 = wrk_f4;*/
2
  • The column names in the commented code are not qualified. Especially in the right hand side of the SET part you have to qualify the column name - just as I have shown in my answer.
    – user1822
    Commented Dec 5, 2018 at 20:55
  • I understand that, and I have qualified (f11 + 1) to (schema_a.table_a.f11 + 1) or (table_a.f11 +1), but that doesn't fix anything. It just complains about f1 instead.
    – E.E
    Commented Dec 6, 2018 at 2:18

1 Answer 1

2

and exactly one field named f6

The column f6 is actually there twice: once for the target table, and once for the excluded record.

The left side of the SET assignment is not ambiguous, but the right side is, as it could mean the excluded value (the one from the values() clause or the original column.

That's why you need to qualify it.

INSERT INTO schema_a.table_a(f1,f2,f3,f4,f5,f6)
VALUES(v1,v2,v3,v4,v5,v6)
ON CONFLICT(f1,f2)
  DO UPDATE SET f6 = table_a.f6 + 1
  --                 ^ 
  --                 this is the ambiguous position
WHERE table_a.f1 = v1 
  and table_a.f2 = v2;
5
  • As stated above, I already tried that. Adding the table results in the complaint that f1 is now ambiguous. Adding schema and table results in the same complaint.
    – E.E
    Commented Dec 5, 2018 at 19:37
  • @E.E: then there is something you are not telling us. The above does work (when all those "v"s are replace with actual constant values): rextester.com/RXERX22913
    – user1822
    Commented Dec 5, 2018 at 19:41
  • No, I'm telling you everything. The above SQL is generalized in that the variable names and table names are not from the code, but the syntax is otherwise EXACT. I will double check it here shortly
    – E.E
    Commented Dec 5, 2018 at 20:21
  • About the only thing extra I can tell you is that the SQL is performed inside of a trigger function.
    – E.E
    Commented Dec 5, 2018 at 20:22
  • I copied the code directly from the database to Sublime, performed some regex replace statements to obfuscate the names. Otherwise, it is everything that I can tell you. Unless you want to me to also print the DDL for the table and obfuscate that.
    – E.E
    Commented Dec 5, 2018 at 20:31

Your Answer

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

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