Rumah > pembangunan bahagian belakang > Golang > ent-go o2m upsert copy

ent-go o2m upsert copy

WBOY
Lepaskan: 2024-02-06 08:57:04
ke hadapan
395 orang telah melayarinya

ent-go o2m upsert 复制

Kandungan soalan

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     |         |
        +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
Salin selepas log masuk

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(),
    }
}
Salin selepas log masuk

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/6vx6y​​ugiwwlchkmssjnpis


Jawapan betul


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

Indeks di atas, digabungkan dengan:

...
.OnConflictColumns(                              
  cmdbciservervmwarevirtualmachinecustomfield.FieldSysId,
  cmdbciservervmwarevirtualmachinecustomfield.FieldKey).
UpdateNewValues().
ID(context.Background())
Salin selepas log masuk

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!

sumber:stackoverflow.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan