go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Feature Request: Add the model.UpdateColumn() to update specified columns.

Open taobig opened this issue 2 years ago • 9 comments

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.

taobig avatar Jun 07 '22 10:06 taobig

You can define model.UpdateStatus func in your model.

re-dylan avatar Jun 12 '22 14:06 re-dylan

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

taobig avatar Jun 13 '22 04:06 taobig

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jul 14 '22 02:07 github-actions[bot]

这个可以有

smithyj avatar Jul 20 '22 01:07 smithyj

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This can have

Issues-translate-bot avatar Jul 20 '22 01:07 Issues-translate-bot

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 19 '22 02:08 github-actions[bot]

goctl 该怎么知道你要更新哪个字段?

How does goctl know what column you want to update?

kesonan avatar Sep 01 '22 15:09 kesonan

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?

Issues-translate-bot avatar Sep 01 '22 15:09 Issues-translate-bot

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

a0v0 avatar Sep 01 '22 15:09 a0v0

//更新某一列的值 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 }

viodoc5 avatar Sep 26 '22 03:09 viodoc5

自己修改模板吧,另外别忘了增加interface

viodoc5 avatar Sep 26 '22 03:09 viodoc5

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

Issues-translate-bot avatar Sep 26 '22 03:09 Issues-translate-bot

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.

yuluyao avatar Mar 06 '24 09:03 yuluyao