go-zero
go-zero copied to clipboard
Feature Request: Add the model.UpdateColumn() to update specified columns.
Sometimes we don't want to update all columns(eg. update status
field). I think we can provide the model.UpdateColumn()
to update the specified columns.
related issue: #1910 , but for my question, I don't want to discuss the dirty attributes.
You can define model.UpdateStatus func in your model.
@re-dylan 是的,这样是可以达到目的,但是需要人工的在每一个model类里面去写一个UpdateColumn()方法,太繁琐和难以维护(如果后续update()的签名变化,我们自定义的方法需要跟着人工一一修改)。 所以希望goctl能够直接提供UpdateColumn()方法;或者是提供自定义的能力,让我们能够自定义模板,实现自动生成此方法。
Yes, you are right. But I think that I need write a UpdateColumn() func manually in every model, it's too complicated and difficult to maintain. So I hope goctl can provide a UpdateColumn() func, or customized capability to enable customized template so that we can auto-generate the func.
This issue is stale because it has been open for 30 days with no activity.
这个可以有
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
This can have
This issue is stale because it has been open for 30 days with no activity.
goctl 该怎么知道你要更新哪个字段?
How does goctl know what column you want to update?
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
How does goctl know which field you want to update?
How does goctl know what column you want to update?
goctl 该怎么知道你要更新哪个字段?
How does goctl know what column you want to update?
maybe we can pass column name as argument to model.UpdateColumn(l.ctx, <columnName>, <newData>)?
//更新某一列的值
func (m *default{{.upperStartCamelObject}}Model) SetColumn(ctx context.Context, column, value string, id int64) error {
{{if .withCache}}data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
{{.keys}}
query := fmt.Sprintf("update %s set %s = %s where ID
= ?", m.table, column, value)
, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
return conn.ExecCtx(ctx, query, id)
},{{.keyValues}})
{{else}}, err := m.conn.ExecCtx(ctx, query, id){{end}}
return err
}
自己修改模板吧,另外别忘了增加interface
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
Modify the template yourself, and don't forget to add the interface
how to modify the template? I add tow files named interface-list.tpl
and list.tpl
in the model dir. Then, I run goctl model
command and find no new method generated in xxxmodel_gen.go
.