Sean
Sean

Reputation: 29790

Distinguishing between inserting and updating with Slick's insertOrUpdate

Is there any way to know whether Slick added a new row to a table or updated an existing row when calling insertOrUpdate?

That function appears to always return 1, which makes a kind of sense, since one row is always affected, either by being created or by being updated. But there doesn't seem to be any way to distinguish between these two cases.

Do I have any alternatives besides rolling my own multi-step upsert routine?

Upvotes: 1

Views: 530

Answers (1)

Cem Catikkas
Cem Catikkas

Reputation: 7174

From http://slick.typesafe.com/doc/3.1.0/queries.html#upserting

val updated = users.insertOrUpdate(User(Some(1), "Admin", "Zeiger"))
// returns: number of rows updated

val updatedAdmin = (users returning users).insertOrUpdate(User(Some(1), "Slick Admin", "Zeiger"))
// returns: None if updated, Some((Int, String)) if row inserted

Upvotes: 3

Related Questions