我定义了以下gorm实体
type Location struct {
gorm.Model
RoomNo string `gorm:"unique_index:roomno_level_buildingid"`
Level string `gorm:"unique_index:roomno_level_buildingid"`
BuildingID string `gorm:"unique_index:roomno_level_buildingid"`
}
type Sensors struct {
gorm.Model
Location Location
LocationID uint
Type string
}
三元组(RoomNo,Level,BuildingID)应唯一。以我的理解,这将是一种“有”的关系。
当我插入具有相同“三元组”的新传感器元组时,如何避免约束冲突?到目前为止,我的方法是查询数据库(它是Postgresql)中现有的“ Locations”三元组,并在“ Sensors”实例内设置(!)适当的字段,调用gorm.DB.Save(...)。