Gorm: How to increase a int column value by one in mysql using gorm?

Created on 30 Oct 2019  路  2Comments  路  Source: go-gorm/gorm

I have a column named order_qty. I want to increase the quantity by some amount. I can not find the suitable update method for this operation.

Equivalent sql query -

update <Table Name> SET order_qty = order_qty +15;

Most helpful comment

Use gorm.Expr().

db.Model(&Test{ID: 1}).Update("order_qty", gorm.Expr("order_qty + ?", 15))

makes

UPDATE `tests` SET `order_qty` = order_qty + 15  WHERE `tests`.`id` = 1

All 2 comments

Use gorm.Expr().

db.Model(&Test{ID: 1}).Update("order_qty", gorm.Expr("order_qty + ?", 15))

makes

UPDATE `tests` SET `order_qty` = order_qty + 15  WHERE `tests`.`id` = 1

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ganitzsh picture Ganitzsh  路  3Comments

koalacxr picture koalacxr  路  3Comments

easonlin404 picture easonlin404  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

fieryorc picture fieryorc  路  3Comments