serial in postgres is being increased even though I added on conflict do nothing

The reason this feels weird to you is that you are thinking of the increment on the counter as part of the insert operation, and therefore the “DO NOTHING” ought to mean “don’t increment anything”. You’re picturing this: Check values to insert against constraint If duplicate detected, abort Increment sequence Insert data But in fact, … Read more

MongoDB: upsert sub-document

No there isn’t really a better solution to this, so perhaps with an explanation. Suppose you have a document in place that has the structure as you show: { “name”: “foo”, “bars”: [{ “name”: “qux”, “somefield”: 1 }] } If you do an update like this db.foo.update( { “name”: “foo”, “bars.name”: “qux” }, { “$set”: … Read more

How can I use use Entity Framework to do a MERGE when I don’t know if the record exists?

I use AddOrUpdate in this situation. However, I believe it does query the database first in order to decide to issue an insert or update. context.MyEntities.AddOrUpdate(e => e.Id, entity); Update: I ran through my debug log files. First it runs: SELECT TOP (2) … WHERE 1 = [Extent1].[Id] Then it runs either: INSERT [dbo].[TestTable](…) VALUES … Read more

Fast or Bulk Upsert in pymongo

Modern releases of pymongo ( greater than 3.x ) wrap bulk operations in a consistent interface that downgrades where the server release does not support bulk operations. This is now consistent in MongoDB officially supported drivers. So the preferred method for coding is to use bulk_write() instead, where you use an UpdateOne other other appropriate … Read more

Bulk insert, update if on conflict (bulk upsert) on Postgres

Turns out a special table named excluded contains the row-to-be-inserted (strange name though) insert into USERS( id, username, profile_picture) select unnest(array[‘12345’]), unnest(array[‘Peter’]), unnest(array[‘someURL’]) on conflict (id) do update set username = excluded.username, profile_picture = excluded.profile_picture; http://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table’s … Read more

How to Perform an UPSERT so that I can use both new and old values in update part

As mentioned in my comment, you don’t have to do the subselect to reference to the row that’s causing ON DUPLICATE KEY to fire. So, in your example you can use the following: INSERT INTO `item` (`item_name`, items_in_stock) VALUES( ‘A’, 27) ON DUPLICATE KEY UPDATE `new_items_count` = `new_items_count` + 27 Remember that most things are … Read more