首页 >后端开发 >Golang >ent-go o2m upsert 复制

ent-go o2m upsert 复制

WBOY
WBOY转载
2024-02-06 08:57:04389浏览

ent-go o2m upsert 复制

问题内容

我正在尝试更新插入表 a,以及与表 b 的关系 - 另外,通过 ent-go 的框架与 postgres 进行更新插入。

cmdbciservervmwarevirtualmachine:
        +----------------+--------------------------------+--------+----------+----------+---------+---------------+-----------+---------------------------------+------------+---------+
        |     field      |              type              | unique | optional | nillable | default | updatedefault | immutable |            structtag            | validators | comment |
        +----------------+--------------------------------+--------+----------+----------+---------+---------------+-----------+---------------------------------+------------+---------+
        | id             | uuid.uuid                      | false  | false    | false    | true    | false         | false     | json:"id,omitempty"             |          0 |         |
        | created_at     | time.time                      | false  | false    | false    | true    | false         | true      | json:"created_at,omitempty"     |          0 |         |
        | updated_at     | time.time                      | false  | false    | false    | true    | true          | false     | json:"updated_at,omitempty"     |          0 |         |
        | deleted_at     | time.time                      | false  | true     | false    | false   | true          | false     | json:"deleted_at,omitempty"     |          0 |         |
        | sysid          | uuid.uuid                      | true   | false    | false    | false   | false         | false     | json:"sysid,omitempty"          |          0 |         |
        | name           | string                         | false  | true     | false    | false   | false         | false     | json:"name,omitempty"           |          0 |         |
        | vcpu           | int                            | false  | true     | false    | false   | false         | false     | json:"vcpu,omitempty"           |          0 |         |
        | corespersocket | int                            | false  | true     | false    | false   | false         | false     | json:"corespersocket,omitempty" |          0 |         |
        | memory         | int                            | false  | true     | false    | false   | false         | false     | json:"memory,omitempty"         |          0 |         |
        | hwversion      | string                         | false  | true     | false    | false   | false         | false     | json:"hwversion,omitempty"      |          0 |         |
        | guestos        | string                         | false  | true     | false    | false   | false         | false     | json:"guestos,omitempty"        |          0 |         |
        | guestosfamily  | string                         | false  | true     | false    | false   | false         | false     | json:"guestosfamily,omitempty"  |          0 |         |
        | guestosfqdn    | string                         | false  | true     | false    | false   | false         | false     | json:"guestosfqdn,omitempty"    |          0 |         |
        | powerstate     | string                         | false  | true     | false    | false   | false         | false     | json:"powerstate,omitempty"     |          0 |         |
        | customfields   | []struct { key int             | false  | false    | false    | false   | false         | false     | json:"customfields,omitempty"   |          0 |         |
        |                | "json:\"key\""; value string   |        |          |          |         |               |           |                                 |            |         |
        |                | "json:\"value\"" }             |        |          |          |         |               |           |                                 |            |         |
        | data           | vmware.vcentervirtualmachine   | false  | true     | false    | false   | false         | false     | json:"data,omitempty"           |          0 |         |
        +----------------+--------------------------------+--------+----------+----------+---------+---------------+-----------+---------------------------------+------------+---------+
        +--------+---------------------------------------------+---------+---------+----------+--------+----------+---------+
        |  edge  |                    type                     | inverse | backref | relation | unique | optional | comment |
        +--------+---------------------------------------------+---------+---------+----------+--------+----------+---------+
        | fields | cmdbciservervmwarevirtualmachinecustomfield | false   |         | o2m      | false  | true     |         |
        +--------+---------------------------------------------+---------+---------+----------+--------+----------+---------+
        
cmdbciservervmwarevirtualmachinecustomfield:
        +------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------------------------+------------+---------+
        |   field    |   type    | unique | optional | nillable | default | updatedefault | immutable |          structtag          | validators | comment |
        +------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------------------------+------------+---------+
        | id         | uuid.uuid | false  | false    | false    | true    | false         | false     | json:"id,omitempty"         |          0 |         |
        | created_at | time.time | false  | false    | false    | true    | false         | true      | json:"created_at,omitempty" |          0 |         |
        | updated_at | time.time | false  | false    | false    | true    | true          | false     | json:"updated_at,omitempty" |          0 |         |
        | deleted_at | time.time | false  | true     | false    | false   | true          | false     | json:"deleted_at,omitempty" |          0 |         |
        | sysid      | uuid.uuid | false  | false    | false    | false   | false         | false     | json:"sysid,omitempty"      |          0 |         |
        | key        | int       | false  | false    | false    | false   | false         | false     | json:"key,omitempty"        |          0 |         |
        | value      | string    | false  | true     | false    | false   | false         | false     | json:"value,omitempty"      |          0 |         |
        +------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------------------------+------------+---------+
        +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
        |      edge      |               type               | inverse | backref | relation | unique | optional | comment |
        +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
        | virtualmachine | cmdbciservervmwarevirtualmachine | true    | fields  | m2o      | true   | true     |         |
        +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+

如您所见,我有我的 edge,并且我的索引对于字段表上的 sys_id 和键是唯一的。

// Indexes of the CmdbCiServerVmwareVirtualMachineCustomField.
func (CmdbCiServerVmwareVirtualMachineCustomField) Indexes() []ent.Index {
    return []ent.Index{
        index.Fields("key", "sysId").
            Edges("virtualMachine").Unique(),
    }
}

以下代码似乎可以正常创建行/关系,但是它不会在第二次运行时更新插入 - 只会重复并在关系列中给出 null 条目。

我正在使用以下代码:

http://pastie.org/p/6vx6y​​ugiwwlchkmssjnpis


正确答案


func (cmdbciservervmwarevirtualmachinecustomfield) indexes() []ent.index {
    return []ent.index{
        index.fields("key", "sysid").unique(),
    }
}

以上索引,结合:

...
.OnConflictColumns(                              
  cmdbciservervmwarevirtualmachinecustomfield.FieldSysId,
  cmdbciservervmwarevirtualmachinecustomfield.FieldKey).
UpdateNewValues().
ID(context.Background())

足以使其正常工作。

以上是ent-go o2m upsert 复制的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除