mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
dialect/sql/sqlgraph: changing the M2M creation semantic from Add to Connect
Thus, calling AddE with the same edge values, ignore and not throw an error. For edge-schemas with extra fields, we maintain the same logic because we cannot determine if the extra fields have different values that what exist in the database
This commit is contained in:
committed by
Ariel Mashraki
parent
4c9d61cf16
commit
aa3d21f01a
@@ -1287,7 +1287,8 @@ func (g *graph) addM2MEdges(ctx context.Context, ids []driver.Value, edges EdgeS
|
||||
columns = edges[0].Columns
|
||||
values = make([]any, 0, len(edges[0].Target.Fields))
|
||||
)
|
||||
// Specs are generated equally for all edges from the same type.
|
||||
// Additional fields, such as edge-schema fields. Note, we use the first index,
|
||||
// because Ent generates the same spec fields for all edges from the same type.
|
||||
for _, f := range edges[0].Target.Fields {
|
||||
values = append(values, f.Value)
|
||||
columns = append(columns, f.Column)
|
||||
@@ -1310,6 +1311,11 @@ func (g *graph) addM2MEdges(ctx context.Context, ids []driver.Value, edges EdgeS
|
||||
}
|
||||
}
|
||||
}
|
||||
// Ignore conflicts only if edges do not contain extra fields, because these fields
|
||||
// can hold different values on different insertions (e.g. time.Now() or uuid.New()).
|
||||
if len(edges[0].Target.Fields) == 0 {
|
||||
insert.OnConflict(sql.DoNothing())
|
||||
}
|
||||
query, args := insert.Query()
|
||||
if err := g.tx.Exec(ctx, query, args, nil); err != nil {
|
||||
return fmt.Errorf("add m2m edge for table %s: %w", table, err)
|
||||
|
||||
Reference in New Issue
Block a user