Why am I getting an ambiguous column in my PG on conflict insert? - postgresql

Here is my query:
insert into zoning_algorithm_value (algorithm_value_id, value, zoning_id)
values (
61,
21,
7321
)
on conflict(algorithm_value_id)
DO
update set value = 21 where zoning_id = 7321 and algorithm_value_id = 61;
I am only referencing one table. Here is the error I am getting.
[42702] ERROR: column reference "zoning_id" is ambiguous
How can it be ambiguous when there is only one table and one column with that name? How do I make this upsert work?

You either need to specify the table name or EXCLUDED to precede the fields in the WHERE clause.
For example, if you only want to update value when the "new" zoning_id and algorithm_value_id are 7321 and 61, respectively:
insert into zoning_algorithm_value (algorithm_value_id, value, zoning_id)
values (61, 21, 7321)
on conflict(algorithm_value_id)
DO
update set value = 21 where EXCLUDED.zoning_id = 7321 and EXCLUDED.algorithm_value_id = 61;
If you instead want the WHERE to reference the "existing" record values, use the table name.

Related

update several columns of a table at once

In postgresql, I want to update several rows of a table according to their id. This doesn't work:
UPDATE table SET othertable_id = 5 WHERE id = 2, 45, 22, 75
What is the correct syntax for this?
Use an IN operator:
update the_table
set othertable_id = 5
where id in (2,45,22,75);

How to update an empty jsonb column using jsonb_set in postgresql?

My purpose is to update a jsonb column using jsonb_set, which is currently null, with an object having more than one key-value pairs. The update command executes successfully but it is not updating anything, the column is still coming up empty. I am trying the following query.
UPDATE tab
set value = jsonb_set(value, '{}', '{"a" : 100, "b" : [100, 200]}'::jsonb)
where id = 100;
Any solutions ?
From what I've understood, it appears you don't need jsonb_set for this case. Simply cast the string to jsonb for updating
UPDATE tab
set value = '{"a" : 100, "b" : [100, 200]}'::jsonb
where id = 100
--and value is null; --additional check if you need.
Demo

Update column with multiple values Postgres

Sample data:
I am trying update a column with values from multiple columns in another table if two columns match.
Consider the following query:
UPDATE application_table
SET asset_list = asset_table.asset_name
FROM asset_table
WHERE application_table.application_name = asset_table.applications;
My table structure is:
application_table:
"asset_list"; "text[]"
"application_name"; "character varying"
asset_table:
"asset_name"; "character varying"
"applications"; "character varying"
I get the following error:
ERROR: column "asset_list" is of type text[] but expression is of type character varying
Line 12 SET asset_list = asset_table.asset_name
What you need to do is aggregate the asset_name per applications value and set asset_list to that aggregated value.
Problem is you can't do something like
UPDATE ..
SET asset_list = ARRAY_AGG(asset_name)
FROM ...
because aggregate functions are not allowed in updates like that.
So here's two other ways to do it:
UPDATE app_table
SET asset_list = _asset_list
FROM (
SELECT applications, ARRAY_AGG(asset_name ORDER BY asset_name) AS _asset_list
FROM asset_table
GROUP BY applications
) AS a
WHERE app_name = applications;
https://www.db-fiddle.com/f/pKB5k6Lexwzqv6ZbCCdJay/0
This first builds a result set of distinct application names and an array of all the asset_names for each of the app names. Then it updates the table as usual with that array value.
Another way is:
UPDATE app_table
SET asset_list = (SELECT ARRAY_AGG(asset_name ORDER BY asset_name)
FROM asset_table
WHERE applications = app_name)
;
https://www.db-fiddle.com/f/8oVWsubXW93n142gtZYLXB/0
This will update every record in app_table, and calculates the array value on the fly for every record.

On Insert: column reference "score" is ambiguous

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?
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

How to insert DB Default values under EF?

when adding a new record like this
ContentContacts c2 = new ContentContacts();
c2.updated_user = c2.created_user = loggedUserId;
c2.created_date = c2.updated_date = DateTime.UtcNow;
db.ContentContacts.AddObject(c2);
I'm getting
Cannot insert the value NULL into column 'main_email_support', table 'SQL2008R2.dbo.ContentContacts'; column does not allow nulls. INSERT fails. The statement has been terminated.
but the default value in the database is an empty string like:
why am I getting such error? shouldn't the EF says something like:
"ohh, it's a nullvalue, so let's add the column default value instead"
I did a small test, created a table with a column that had a default value but did not allow nulls.
Then this SQL Statement:
INSERT INTO [Test].[dbo].[Table_1]
([TestText])
VALUES
(null)
Gives this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'TestText', table
'Test.dbo.Table_1'; column does not allow nulls. INSERT fails.
The problem here is that the insert specifies all the columns, also those with default values. The the Insert tries to update the columns with null values.
You have 2 options:
Update the table through a view, which does not contain the default columns
Set the default values in your C# code
A default value is business logic, so there is a case for it being set in the business layer of your application.