postgresql ent-go o2 m上插复制

vzgqcmou  于 2023-02-08  发布在  PostgreSQL
关注(0)|答案(1)|浏览(159)

我试图用表B的关系来upsert表A--同样,用Postgres通过ent-go的框架来upsert。

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,并且我的索引对于fields表上的sys_id和key是唯一的。

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

下面的代码看起来创建了行/关系,但是它在第二次运行时不会upsert--只会复制并在关系列中给出一个NULL条目。
我使用下面的代码:
http://pastie.org/p/6vx6yugIWwlChKmSSJNPIs

ozxc1zmp

ozxc1zmp1#

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

以上指标,结合:

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

足以让它正常工作。

相关问题