Gorm: Expose `For Update` in a transaction context

Created on 23 Dec 2015  Â·  7Comments  Â·  Source: go-gorm/gorm

Many times, when I perform an update, I use for update on a row to guarantee its synchronicity system wide. This is actually a very common thing to do within a transaction. Could something like this be added to the api? Here is an example of how I would like to use it:

tx := db.Begin()
var model Model
if err := tx.First(&model).ForUpdate().Error; err != nil { /* SELECT FIRST * FROM model WHERE id = ? FOR UPDATE */
    log.Println(err)
    tx.Rollback()
    return err
}
model.Field = "update this field in a synchronized way"
if err := tx.Save(&model).Error; err != nil {
    log.Println(err)
    tx.Rollback()
    return err
}
tx.Commit()
feature

Most helpful comment

@huangzhiran
这里 Extra Querying option

// Add extra SQL option for selecting SQL
db.Set("gorm:query_option", "FOR UPDATE").First(&user, 10)
//// SELECT * FROM users WHERE id = 10 FOR UPDATE;

All 7 comments

:+1:

@jinzhu hi, I see you marked a v1.0 milestone, would you please wrtite a plan (structures refactor or features and so on) in a single issue to track it ?

@jinzhu Ok, it is in #794

Excellent!

On Feb 15, 2016, at 8:32 AM, Jinzhu [email protected] wrote:

Closed #769 https://github.com/jinzhu/gorm/issues/769.

—
Reply to this email directly or view it on GitHub https://github.com/jinzhu/gorm/issues/769#event-550678974.

@marksalpeter how can i use "select for update" with gorm, I cannot find description in http://gorm.io/docs/
thanks

@huangzhiran
这里 Extra Querying option

// Add extra SQL option for selecting SQL
db.Set("gorm:query_option", "FOR UPDATE").First(&user, 10)
//// SELECT * FROM users WHERE id = 10 FOR UPDATE;

@huangzhiran sorry I didn't see this sooner
http://gorm.io/docs/query.html#Extra-Querying-option

Was this page helpful?
0 / 5 - 0 ratings

Related issues

youtwo123 picture youtwo123  Â·  3Comments

hypertornado picture hypertornado  Â·  3Comments

alanyuen picture alanyuen  Â·  3Comments

Quentin-M picture Quentin-M  Â·  3Comments

kumarsiva07 picture kumarsiva07  Â·  3Comments