How can I create a recursive association using GORM?

huangapple go评论66阅读模式
英文:

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:&quot;many2many:manual_auto&quot;.
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:&quot;many2many:manual_auto&quot;`
}

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:&quot;many2many:manual_auto&quot;`
}

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:&quot;column:equipment_id&quot;`
    Ip        string      `gorm:&quot;column:manage_ip&quot;`
    Equipment []Equipment `gorm:&quot;many2many:manual_auto&quot;`
}

huangapple
  • 本文由 发表于 2017年8月30日 21:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/45961815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定