"upsert" vs "insert_or_update" namings

In Ecto we can find two namings for an operation that either inserts or updates.
To my understanding:

  • Insert_or_update operation: inserts or updates whether an ID is present in the given schema

  • Upsert: tries to insert, if unique DB constraint error is returned from database, then updates

Is this distinction between these two namings made up by Elixir/Ecto? Or is this distinction also present in other frameworks?

What would you name your context functions?
insert_or_update_foo or upsert_foo?
I guess for the context’s API we must choose only one of those, because the strategy doesn’t have to be leaked and have different namings.

Upserts are a database feature of doing an update when conflicts are detected (conflicts is broader then just “exists”), while insert_or_update will simply look at the metadata of the ecto schema within a changeset to determine if insert or update is appropriate.

1 Like