Saya cuba mengemas kini sisipan ke dalam jadual a, dan hubungan dengan jadual b - juga, sisipan kemas kini melalui rangka kerja ent-go dengan 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 | | +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
Seperti yang anda lihat, saya mempunyai kelebihan saya dan indeks saya unik untuk sys_id dan kunci pada jadual medan.
// Indexes of the CmdbCiServerVmwareVirtualMachineCustomField. func (CmdbCiServerVmwareVirtualMachineCustomField) Indexes() []ent.Index { return []ent.Index{ index.Fields("key", "sysId"). Edges("virtualMachine").Unique(), } }
Kod berikut nampaknya mencipta baris/perhubungan yang baik, namun ia tidak mengemas kini sisipan pada larian kedua - ia hanya berulang dan memberikan entri nol dalam lajur perhubungan.
Saya menggunakan kod berikut:
http://pastie.org/p/6vx6yugiwwlchkmssjnpis
func (cmdbciservervmwarevirtualmachinecustomfield) indexes() []ent.index { return []ent.index{ index.fields("key", "sysid").unique(), } }
Indeks di atas, digabungkan dengan:
... .OnConflictColumns( cmdbciservervmwarevirtualmachinecustomfield.FieldSysId, cmdbciservervmwarevirtualmachinecustomfield.FieldKey). UpdateNewValues(). ID(context.Background())
Cukup untuk menjadikannya berkesan.
Atas ialah kandungan terperinci ent-go o2m upsert copy. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!