Home > Article > Backend Development > ent-go o2m upsert copy
I am trying to update the insert into table a, and the relationship with table b - In addition, update through the ent-go framework with postgres insert.
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 | | +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
As you can see, I have my edge and my index is unique for the sys_id and key on the fields table.
// Indexes of the CmdbCiServerVmwareVirtualMachineCustomField. func (CmdbCiServerVmwareVirtualMachineCustomField) Indexes() []ent.Index { return []ent.Index{ index.Fields("key", "sysId"). Edges("virtualMachine").Unique(), } }
The following code seems to create the row/relationship fine, however it does not update the insert on the second run - it just repeats and gives a null entry in the relationship column.
I am using the following code:
http://pastie.org/p/6vx6yugiwwlchkmssjnpis
func (cmdbciservervmwarevirtualmachinecustomfield) indexes() []ent.index { return []ent.index{ index.fields("key", "sysid").unique(), } }
The above index, combined with:
... .OnConflictColumns( cmdbciservervmwarevirtualmachinecustomfield.FieldSysId, cmdbciservervmwarevirtualmachinecustomfield.FieldKey). UpdateNewValues(). ID(context.Background())
is enough to make it work.
The above is the detailed content of ent-go o2m upsert copy. For more information, please follow other related articles on the PHP Chinese website!