你可以看到我执行了一次更新操作BelongsTo=7
但是接下来发生了我始料未及的问题,更新没有成功
以下是我打印的sql,belongs_to=3值被还原了
UPDATE `coupon` SET `created_at`='2024-01-08 17:37:04.988',`updated_at`='2024-01-29 17:46:32.718',`deleted_at`=NULL,`name`='店铺2的优惠券22222',`benefits_brief`='123123',`benefits_details`='店铺2的优惠券',`amount`=20,`coupon_type`='2',`logo`='uploads/file/c4ca4238a0b923820dcc509a6f75849b_20240112175412.png',`belongs_to`=3,`stock`=16,`time_info`='{"startedAt":"2024-01-15T02:56:03Z","endAt":"2024-03-01T02:56:03Z","timeType":1,"relativeTime":0}',`attributes`='{"icon":""}' WHERE `coupon`.`deleted_at` IS NULL AND `id` = 2
这是我定义的结构体
// 自动生成模板Coupon
package shop
import (
"database/sql/driver"
"encoding/json"
"github.com/flipped-aurora/gin-vue-admin/server/constants"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/types"
)
// 优惠券 结构体 Coupon
type Coupon struct {
global.GVA_MODEL
Name string `json:"name" form:"name" gorm:"column:name;comment:名称;size:20"` //名称
BenefitsBrief string `json:"benefitsBrief" form:"benefitsBrief" gorm:"column:benefits_brief;comment:;type:longtext;"` // 权益简介
BenefitsDetails string `json:"benefitsDetails" form:"benefitsDetails" gorm:"column:benefits_details;comment:;type:longtext;"` // 权益详情
Amount int64 `json:"amount" form:"amount" gorm:"column:amount;comment:金额;"` //金额
CouponType constants.CouponType `json:"couponType" form:"couponType" gorm:"column:coupon_type;comment:优惠券类型;"` //优惠券类型
Logo string `json:"logo" form:"logo" gorm:"column:logo;comment:logo;"` //logo
BelongsTo *uint `json:"belongsTo" form:"belongsTo" gorm:"column:belongs_to;comment:从属外键;"` //从属外键
Stock int `json:"stock" form:"stock" gorm:"column:stock;comment:库存;"` //库存
TimeInfo types.TimeInfo `json:"timeInfo" form:"timeInfo" gorm:"column:time_info;comment:时间信息;type:json"` //时间信息
Attributes Attributes `json:"attributes" form:"attributes" gorm:"column:attributes;comment:相关属性;type:json"` //相关属性
Business *Business `json:"business" form:"business" gorm:"foreignKey:BelongsTo;references:ID;"`
}
// Attributes 相关属性
type Attributes struct {
Icon string `json:"icon" form:"icon" gorm:"column:icon;comment:图标;"` //图标
}
func (a Attributes) Value() (driver.Value, error) {
return json.Marshal(&a)
}
func (a *Attributes) Scan(value interface{}) error {
return json.Unmarshal(value.([]byte), &a)
}
// TableName 优惠券 Coupon自定义表名 coupon
func (Coupon) TableName() string {
return "coupon"
}
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: Madou-Shinni
我已经解决了这个问题,通过跳过关联保存的方式 https://gorm.io/zh_CN/docs/associations.html#Using-Omit-to-Exclude-Fields-or-Associations