upsert

Return rows from INSERT with ON CONFLICT without needing to update

心不动则不痛 提交于 2019-11-28 09:06:21
I have a situation where I very frequently need to get a row from a table with a unique constraint, and if none exists then create it and return. For example my table might be: CREATE TABLE names( id SERIAL PRIMARY KEY, name TEXT, CONSTRAINT names_name_key UNIQUE (name) ); And it contains: id | name 1 | bob 2 | alice Then I'd like to: INSERT INTO names(name) VALUES ('bob') ON CONFLICT DO NOTHING RETURNING id; Or perhaps: INSERT INTO names(name) VALUES ('bob') ON CONFLICT (name) DO NOTHING RETURNING id and have it return bob's id 1 . However, RETURNING only returns either inserted or updated

How to find out if an upsert was an update with PostgreSQL 9.5+ UPSERT?

若如初见. 提交于 2019-11-28 09:03:47
Writable CTEs were considered a solution to UPSERT prior to 9.5 as described in Insert, on duplicate update in PostgreSQL? It is possible to perform an UPSERT with the information whether it ended up as an UPDATE or an INSERT with the following Writable CTEs idiom: WITH update_cte AS ( UPDATE t SET v = $1 WHERE id = $2 RETURNING 'updated'::text status ), insert_cte AS ( INSERT INTO t(id, v) SELECT $2, $1 WHERE NOT EXISTS (SELECT 1 FROM update_cte) RETURNING 'inserted'::text status ) (SELECT status FROM update_cte) UNION (SELECT status FROM insert_cte) This query will return either "updated" or

Use multiple conflict_target in ON CONFLICT clause

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:08:11
I have two columns in table col1 , col2 , they both are unique indexed (col1 is unique and so is col2). I need at insert into this table, use ON CONFLICT syntax and update other columns, but I can't use both column in conflict_target clause. It works: INSERT INTO table ... ON CONFLICT ( col1 ) DO UPDATE SET -- update needed columns here But how to do this for several columns, something like this: ... ON CONFLICT ( col1, col2 ) DO UPDATE SET .... e4c5 A sample table and data CREATE TABLE dupes(col1 int primary key, col2 int, col3 text, CONSTRAINT col2_unique UNIQUE (col2) ); INSERT INTO dupes

MySQL “good” way to insert a row if not found, or update it if it is found

余生颓废 提交于 2019-11-28 05:59:43
Very often, I want to run a query on one of my users where I want a row stored and associated with that user, in a 1-to-1 relationship. So let's say (this is just an arbitrary example), that I have a table that keeps track of a user's car, along with some info about the car. Each user can have either 0 or 1 cars. If the user has no car, there is no entry in the table for that user. cars table (again, just an example): id, user_id, car_make, car_model So, when I update this table, I always end up doing something like this (pseudo-code): result = SELECT * FROM cars WHERE user_id=5 if (num_rows

Elasticsearch upserting and appending to array

蹲街弑〆低调 提交于 2019-11-28 04:32:42
I'm trying to write a script that will upsert a new user record to ElasticSearch, updating any information if the user already exists, and appending a new PaymentInfo object to the user's Payments array if it exists in the update object. Here's a simplified version of what I'm working with so far: curl -XPOST 'http://localhost:9200/usrtest/usr/1/_update' -d ' { "doc_as_upsert": true, "doc": { "customerId": "1", "firstName": "Mark", "lastName": "Z", "emailAddress": "foo.bar@gmail.com", "paymentInfo": { "pid": "1", "amt": "10" } } }' This almost does what I want in that it inserts the doc

postgres syntax error at or near “ON”

醉酒当歌 提交于 2019-11-28 04:08:41
问题 I created this table: CREATE TABLE IF NOT EXISTS config_activity_log ( id serial primary key, activity_name varchar(100) NOT NULL, last_config_version varchar(50) NOT NULL, activity_status varchar(100) NOT NULL DEFAULT 'Awaiting for cofman', cofman_last_update bigint NOT NULL DEFAULT -1, is_error boolean DEFAULT FALSE, activity_timestamp timestamp DEFAULT current_timestamp ); I try to run this postgres script: INSERT INTO config_activity_log (activity_name, last_config_version, activity

Detect if the row was updated or inserted

回眸只為那壹抹淺笑 提交于 2019-11-28 02:18:02
问题 I am doing an INSERT with ON CONFLICT to postgre using java. Is there any way to find out if the executeUpdate inserted the row or updated it? 回答1: You can look at the system column xmax to tell the difference. It's 0 for inserted rows in this case. CREATE TABLE tbl(id int PRIMARY KEY, col int); INSERT INTO tbl VALUES (1, 1); INSERT INTO tbl(id, col) VALUES (1,11), (2,22) ON CONFLICT (id) DO UPDATE SET col = EXCLUDED.col RETURNING *, (xmax = 0) AS inserted ; This is building on an

Oracle: ON DUPLICATE KEY UPDATE [duplicate]

纵饮孤独 提交于 2019-11-28 02:04:43
This question already has an answer here: Oracle: how to UPSERT (update or insert into a table?) 12 answers I'm trying to implement a solution I found over here from Michiel de Mare to update multiple records with one (preferably-simple-in-a-syntax-sense) query. The example code that I am trying to learn from looks like this: INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12) ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2); I'm using Oracle (and am not yet well versed in SQL queries). Based on some dynamic content, I have concatenated my query similar to the

MySql upsert and auto-increment causes gaps

佐手、 提交于 2019-11-28 01:29:44
I've got a MySql table with an auto-increment primary key, and it seems that all of the various upsert methods (INSERT IGNORE and ON DUPLICATE KEY UPDATE) suffer from the, uh, feature that the auto-increment field increments, even if a row is updated and not inserted. This means that gaps are introduced into the table, which I find undesirable. So the question is: is there any way to upsert records in a table with an auto-increment field without auto-incrementing that field, if the upsert in fact merely updates the row. To my mind, this is the way upsert should behave, but it doesn't seem to.

Upsert Array Elements matching criteria in a MongoDB document?

故事扮演 提交于 2019-11-27 23:52:56
As per How do I update Array Elements matching criteria in a MongoDB document? I want to upsert the array elements, so if one doesnt match then insert it, otherwise update it. I tried the answer on that question, and it works fine IF the array element already exists. If the element doesnt exist then it creates a child of "$" under the array field. My Mongo structure is as follows: Widget (collection) --Name --Properties (array) --Name --Value My application gets a Widget Name and a list of Properties from a WebService call. I wish to iterate the provided Properties and update the value in the