ent-go o2m upsert copy

WBOY
Release: 2024-02-06 08:57:04
forward
347 people have browsed it

ent-go o2m upsert 复制

Question content

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     |         |
        +----------------+----------------------------------+---------+---------+----------+--------+----------+---------+
Copy after login

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(),
    }
}
Copy after login

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


Correct answer


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

The above index, combined with:

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

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!