Gorm: How do we know if update was successful?

Created on 26 May 2014  ·  3Comments  ·  Source: go-gorm/gorm

Using postgres adapter, how do we know if an update was successful?

update users set email='[email protected]' where id=2 (i.e. db.Table("users").Where(2).Updates(map))

If id=2 does not exist, then postgres will return "UPDATE 0".

I would consider any client using a non-existent id in an update statement to be an error. I would want to send back this error to client.

Is there any way to know the updated number of rows for an update using gorm? If updated number of rows is 0 (and client used an id to update), then we can send back an error.

Most helpful comment

Hi @satb

Thank you for your feedback, already added this support, you could get the affected columns like this:

fmt.Println(db.Table("users").Where(2).Updates(map).RowsAffected)

All 3 comments

Hi @satb

Thank you for your feedback, already added this support, you could get the affected columns like this:

fmt.Println(db.Table("users").Where(2).Updates(map).RowsAffected)

@jinzhu Hi, how to check errors with affected rows ?

Thanks

Hi @satb

Thank you for your feedback, already added this support, you could get the affected columns like this:

fmt.Println(db.Table("users").Where(2).Updates(map).RowsAffected)

我按照你这个写法,还是不对,返回的值永远是 0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  ·  3Comments

bramp picture bramp  ·  3Comments

RadhikaBhat picture RadhikaBhat  ·  3Comments

superwf picture superwf  ·  3Comments

pjebs picture pjebs  ·  3Comments