Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose For Update in a transaction context #769

Closed
marksalpeter opened this issue Dec 23, 2015 · 7 comments
Closed

Expose For Update in a transaction context #769

marksalpeter opened this issue Dec 23, 2015 · 7 comments

Comments

@marksalpeter
Copy link

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()
@jinzhu jinzhu added this to the v1.0 milestone Jan 10, 2016
@derekperkins
Copy link

👍

@douglarek
Copy link

@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 ?

@douglarek
Copy link

@jinzhu Ok, it is in #794

@marksalpeter
Copy link
Author

Excellent!

On Feb 15, 2016, at 8:32 AM, Jinzhu notifications@github.com 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.

@huangzhiran
Copy link

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

@burtbai
Copy link

burtbai commented Apr 1, 2019

@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;

@marksalpeter
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants