bun icon indicating copy to clipboard operation
bun copied to clipboard

Upsert with `RETURNING("*")` and `ON CONFLICT` doesn't return `sql.ErrNoRows`

Open ardenn opened this issue 2 years ago • 1 comments

Hello, thank you for the great job with bun. I have come across a scenario which may be a bug/regression from go-pg. With go-pg, performing an UPSERT with a RETURNING clause would return pg.ErrNoRows in the event of a DB conflict. Which allowed having a pattern like so:

if _, err := db.Model(&model).OnConflict("(name) DO NOTHING").Returning("*").Insert(); err != nil {
	if err == pg.ErrNoRows {
		// Conflict found
		return nil, fmt.Errorf("duplicate data")
	}

As per the migrating to go-pg docs, pg.ErrNoRows is replaced by sql.ErrNoRows, however result, err := db.NewInsert().Model(&model).Ignore().Returning("*").Exec(ctx) doesn't appear to return any error at all when there is a conflict but instead returns sql.Result with 0 RowsAffected(). Is this an intended behaviour?

ardenn avatar Jun 02 '22 16:06 ardenn

@vmihailenco is this valid issue?

VladimirStepanov avatar Jun 16 '22 14:06 VladimirStepanov

Answered in #752

ardenn avatar Jan 26 '23 17:01 ardenn