The following section covers some common scenarios:
Anorm throws an error at the
SqlMappingError
runtime (too many rows when you're expecting a single one), even though the query resulted in expected behavior. It is an insert query using "on duplicate key update".This can happen when such a query is being executed using
executeInsert
. TheexecuteInsert
method should be used when we need to return an autogenerated key. If we are updating some fields through a duplicate key, it means that we do not actually need the key. We could useexecuteUpdate
to add a check if one row has been updated. For example, we may want to update the wishlist table, which tracks what a user has wished for:DB.withConnection { implicit connection => { val updatedRows = SQL"""INSERT INTO wish_list (user_id, product_id, liked_at) VALUES ($userId,$productId,$likedAt) ON DUPLICATE KEY UPDATE liked_at=$likedAt, is_deleted=false """.executeUpdate() updatedRows == 1 } }
Can we use multiple...