英文:
How can I create a recursive association using GORM?
问题
如何使用GORM创建递归关联?
我有一个设备,可以手动定义,也可以自动定义。我想将手动定义的设备与自动定义的设备关联起来。
所以我决定创建一个用于这种多对多关联的表。
CREATE TABLE `equipment` (
`equipment_id` int(11) NOT NULL,
`manage_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `equipment`
ADD PRIMARY KEY (`equipment_id`),
ADD UNIQUE KEY `unique_equipment` (`model_id`,`manage_ip`);
CREATE TABLE `manual_auto` (
`manual_id` int(11) NOT NULL,
`auto_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `manual_auto`
ADD PRIMARY KEY (`manual_id`,`auto_id`),
ADD KEY `FK_AUTO` (`auto_id`);
ALTER TABLE `manual_auto`
ADD CONSTRAINT `FK_AUTO` FOREIGN KEY (`auto_id`) REFERENCES `equipment` (`equipment_id`),
ADD CONSTRAINT `FK_MANUAL` FOREIGN KEY (`manual_id`) REFERENCES `equipment` (`equipment_id`);
我知道可以使用gorm:"many2many:manual_auto"
来创建多对多关联表。
有关GORM关联的更多信息,请参考:http://jinzhu.me/gorm/associations.html#belongs-to
type Equipment struct {
ID uint
Ip string
Equipment Equipment `gorm:"many2many:manual_auto"`
}
提前谢谢你。
编辑:关于上下文的更多信息
我想要有两个版本的设备,一个是通过从另一个数据库获取自动创建的。然后,如果任何用户对设备的描述进行修改,我希望有这个设备的两个版本,即"manual_equipement"和"automatic_equipment"。
英文:
Question: How can I create a recursive association using GORM?
Context: I have an equipment that can be define manually as well as automatically. I want to associate a manual equipment with the automatically defined one.
So I decided to create a table for this many2many association.
<!-- language: lang-sql-->
CREATE TABLE `equipment` (
`equipment_id` int(11) NOT NULL,
`manage_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `equipment`
ADD PRIMARY KEY (`equipment_id`),
ADD UNIQUE KEY `unique_equipment` (`model_id`,`manage_ip`);
CREATE TABLE `manual_auto` (
`manual_id` int(11) NOT NULL,
`auto_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `manual_auto`
ADD PRIMARY KEY (`manual_id`,`auto_id`),
ADD KEY `FK_AUTO` (`auto_id`);
ALTER TABLE `manual_auto`
ADD CONSTRAINT `FK_AUTO` FOREIGN KEY (`auto_id`) REFERENCES `equipment` (`equipment_id`),
ADD CONSTRAINT `FK_MANUAL` FOREIGN KEY (`manual_id`) REFERENCES `equipment` (`equipment_id`);
I know that I can create a many2many association table using gorm:"many2many:manual_auto"
.
More info on associations with GORM: http://jinzhu.me/gorm/associations.html#belongs-to
<!-- language: lang-go -->
type Equipment struct {
ID uint
Ip string
Equipment Equipment `gorm:"many2many:manual_auto"`
}
Thank you in advance.
Edit: More information about the context
I want to have two version of an equipment, one will be created automaticity by fetching another database. Then if any user make a modification about the description of the equipment I want to have 2 version of this equipment, "manual_equipement" and "automatic_equipment".
答案1
得分: 1
当需要表示“多个”时,你需要使用数组/切片:
type Equipment struct {
ID uint
Ip string
Equipment []Equipment `gorm:"many2many:manual_auto"`
}
此外,你手动创建的模式的名称与结构体中的名称不匹配。或者你可以使用 db.AutoMigrate 或在 gorm 标签中定义列名:
type Equipment struct {
ID uint `gorm:"column:equipment_id"`
Ip string `gorm:"column:manage_ip"`
Equipment []Equipment `gorm:"many2many:manual_auto"`
}
英文:
When it's "many", then you need an array/slice:
type Equipment struct {
ID uint
Ip string
Equipment []Equipment `gorm:"many2many:manual_auto"`
}
also names of your manually created schema does not match names from struct. Or you can use db.AutoMigrate or define column names in gorm tags:
type Equipment struct {
ID uint `gorm:"column:equipment_id"`
Ip string `gorm:"column:manage_ip"`
Equipment []Equipment `gorm:"many2many:manual_auto"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论