修复软删除和唯一索引同时存在的bug

This commit is contained in:
2025-11-20 17:37:02 +08:00
parent 1f3d3d8a7c
commit da934a9bbb
7 changed files with 80 additions and 8 deletions

View File

@@ -21,11 +21,11 @@ type AreaController struct {
Model
// Name 是主控的业务名称,例如 "1号猪舍主控"
Name string `gorm:"not null;unique" json:"name"`
Name string `gorm:"not null" json:"name"`
// NetworkID 是主控在通信网络中的唯一标识,例如 LoRaWAN 的 DevEUI。
// 这是 transport 层用来寻址的关键。
NetworkID string `gorm:"not null;unique;index" json:"network_id"`
NetworkID string `gorm:"not null;index" json:"network_id"`
// Location 描述了主控的物理安装位置。
Location string `gorm:"index" json:"location"`

View File

@@ -108,7 +108,7 @@ type DeviceTemplate struct {
Model
// Name 是此模板的唯一名称, 例如 "FanModel-XYZ-2000" 或 "TempSensor-T1"
Name string `gorm:"not null;unique" json:"name"`
Name string `gorm:"not null" json:"name"`
// Manufacturer 是设备的制造商。
Manufacturer string `json:"manufacturer"`

View File

@@ -7,7 +7,7 @@ package models
// PigHouse 定义了猪舍,是猪栏的集合
type PigHouse struct {
Model
Name string `gorm:"size:100;not null;unique;comment:猪舍名称, 如 '育肥舍A栋'"`
Name string `gorm:"size:100;not null;comment:猪舍名称, 如 '育肥舍A栋'"`
Description string `gorm:"size:255;comment:描述信息"`
Pens []Pen `gorm:"foreignKey:HouseID"` // 一个猪舍包含多个猪栏
}

View File

@@ -31,7 +31,7 @@ const (
// PigBatch 是猪批次的核心模型,代表了一群被共同管理的猪
type PigBatch struct {
Model
BatchNumber string `gorm:"size:50;not null;uniqueIndex;comment:批次编号,如 2024-W25-A01"`
BatchNumber string `gorm:"size:50;not null;comment:批次编号,如 2024-W25-A01"`
OriginType PigBatchOriginType `gorm:"size:20;not null;comment:批次来源 (自繁, 外购)"`
StartDate time.Time `gorm:"not null;comment:批次开始日期 (如转入日或购买日)"`
EndDate time.Time `gorm:"not null;comment:批次结束日期 (全部淘汰或售出)"`

View File

@@ -21,7 +21,7 @@ const (
// RawMaterial 代表一种原料的静态定义,是系统中的原料字典。
type RawMaterial struct {
Model
Name string `gorm:"size:100;unique;not null;comment:原料名称"`
Name string `gorm:"size:100;not null;comment:原料名称"`
Description string `gorm:"size:255;comment:描述"`
// RawMaterialNutrients 关联此原料的所有营养素含量信息
RawMaterialNutrients []RawMaterialNutrient `gorm:"foreignKey:RawMaterialID"`
@@ -36,7 +36,7 @@ func (RawMaterial) TableName() string {
// 约定:宏量营养素(粗蛋白等)单位为百分比(%),微量元素(氨基酸等)单位为毫克/千克(mg/kg)。
type Nutrient struct {
Model
Name string `gorm:"size:100;unique;not null;comment:营养素名称"`
Name string `gorm:"size:100;not null;comment:营养素名称"`
Description string `gorm:"size:255;comment:描述"`
// RawMaterialNutrients 记录营养在哪些原料中存在且比例是多少
RawMaterialNutrients []RawMaterialNutrient `gorm:"foreignKey:NutrientID"`

View File

@@ -44,7 +44,7 @@ type User struct {
// Username 是用户的登录名,应该是唯一的
// 修正了 gorm 标签的拼写错误 (移除了 gorm 后面的冒号)
Username string `gorm:"unique;not null" json:"username"`
Username string `gorm:"not null" json:"username"`
// Password 存储的是加密后的密码哈希,而不是明文
// json:"-" 标签确保此字段在序列化为 JSON 时被忽略,防止密码泄露