Skip to content

Generics API: Updates ( ctx, t ) and Hook cannot be used together #7713

@Expecting-luck

Description

@Expecting-luck

Description

Updates ( ctx, t ) and Hook cannot be used together
Updates ( ctx, t ) 与 Hook 无法一起使用

entity

type CompanyProject struct {
	global.QslModelUUID
	BindId       *string `gorm:"size:36;not null;default:'';comment:绑定ID"`
	Name         *string `gorm:"size:120;not null;default:'';comment:项目名称"`
	Department   *string `gorm:"size:50;not null;default:'';comment:所属部门"`
	Company      *string `gorm:"size:50;not null;default:'';comment:所属公司"`
	Description  *string `gorm:"size:500;not null;default:'';comment:项目描述"`
	ProjectAnnex *string `gorm:"size:90000;not null;default:'[]';comment:项目附件"`
	Content      *string `gorm:"size:90000;not null;default:'';comment:项目内容"`
	Sort         *uint   `gorm:"not null;default:0;comment:排序"`
}

func (*CompanyProject) TableName() string {
	return "company_project"
}

// BeforeUpdate 钩子 - 修改前自动填充修改人账号
func (c *CompanyProject) BeforeUpdate(tx *gorm.DB) (err error) {
	// 从 GORM 的 Context 中获取账号信息
	if ctxValue := tx.Statement.Context.Value("account"); ctxValue != nil {
		if account, ok := ctxValue.(string); ok {
			c.UpdatedAccount = account
		}
	}
	return
}

service

func (companyProjectService *CompanyProjectService) UpdateCompanyProject(companyProjectModel business.CompanyProject, account string) (err error) {
	ctx := baseContext.WithValue(baseContext.Background(), "account", account)
	_, err = gorm.G[business.CompanyProject](global.QslDb).Where("id = ?", companyProjectModel.ID).
		Omit("id", "created_at", "created_account", "deleted_at", "deleted_account", "bind_id").
		Updates(ctx, companyProjectModel)
	return err
}

service err: invalid value, should be pointer to struct or slice

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions