yiGmMk / comment_blog

the comments of www_programnotes_cn
0 stars 0 forks source link

using gorm | ProgramNotes #80

Open yiGmMk opened 1 year ago

yiGmMk commented 1 year ago

https://programnotes.cn/using-gorm/index.html

CURDCreatejson自定义字段使用 如下为system使用自定义字段123type System struct{ Version string} 为自定义类型实现接口Value/Scan123456789101112131415161718192021222324 func (s System)Value()(driver.Value, error) {

ghost commented 10 months ago

嵌套预加载的时候,排序是否不生效。试过嵌套排序还是链式调用,sql里面虽然带上了排序条件 但是返回来的数据结果集并没有排序。可能是因为嵌套两层的原因吗

yiGmMk commented 10 months ago

嵌套预加载的时候,排序是否不生效。试过嵌套排序还是链式调用,sql里面虽然带上了排序条件 但是返回来的数据结果集并没有排序。可能是因为嵌套两层的原因吗

支持排序,在Preload里需要添加排序参数,像这样 ` // 文档:https://gorm.io/zh_CN/docs/preload.html //您可以通过 func(db gorm.DB) gorm.DB 实现自定义预加载 SQL,例如:

db.Preload("Users", func(db gorm.DB) gorm.DB { return db.Order("users.age DESC") }).Find(&orders) `