Skip to content

Commit

Permalink
entc/integration: add example for composite unique index (#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Feb 6, 2023
1 parent 9881f57 commit 8022271
Show file tree
Hide file tree
Showing 18 changed files with 1,167 additions and 9 deletions.
9 changes: 8 additions & 1 deletion dialect/sql/builder.go
Expand Up @@ -949,6 +949,13 @@ func (u *UpdateSet) SetExcluded(name string) *UpdateSet {

// Query returns query representation of an `INSERT INTO` statement.
func (i *InsertBuilder) Query() (string, []any) {
query, args, _ := i.QueryErr()
return query, args
}

// QueryErr returns query representation of an `INSERT INTO`
// statement and any error occurred in building the statement.
func (i *InsertBuilder) QueryErr() (string, []any, error) {
b := i.Builder.clone()
b.WriteString("INSERT INTO ")
b.writeSchema(i.schema)
Expand All @@ -969,7 +976,7 @@ func (i *InsertBuilder) Query() (string, []any) {
i.writeConflict(&b)
}
joinReturning(i.returning, &b)
return b.String(), b.args
return b.String(), b.args, b.Err()
}

func (i *InsertBuilder) writeDefault(b *Builder) {
Expand Down
18 changes: 12 additions & 6 deletions dialect/sql/sqlgraph/graph.go
Expand Up @@ -1050,7 +1050,10 @@ func (c *creator) node(ctx context.Context, drv dialect.Driver) error {
// we interact with an edge-schema with composite primary key.
if c.ID == nil {
c.ensureConflict(insert)
query, args := insert.Query()
query, args, err := insert.QueryErr()
if err != nil {
return err
}
return c.tx.Exec(ctx, query, args, nil)
}
if err := c.insert(ctx, insert); err != nil {
Expand Down Expand Up @@ -1096,7 +1099,10 @@ func (c *creator) insert(ctx context.Context, insert *sql.InsertBuilder) error {
// In case of "ON CONFLICT", the record may exist in the
// database, and we need to get back the database id field.
if len(c.CreateSpec.OnConflict) == 0 {
query, args := insert.Query()
query, args, err := insert.QueryErr()
if err != nil {
return err
}
return c.tx.Exec(ctx, query, args, nil)
}
}
Expand Down Expand Up @@ -1529,8 +1535,8 @@ func setTableColumns(fields []*FieldSpec, edges map[Rel][]*EdgeSpec, set func(st

// insertLastID invokes the insert query on the transaction and returns the LastInsertID.
func (c *creator) insertLastID(ctx context.Context, insert *sql.InsertBuilder) error {
query, args := insert.Query()
if err := insert.Err(); err != nil {
query, args, err := insert.QueryErr()
if err != nil {
return err
}
// MySQL does not support the "RETURNING" clause.
Expand Down Expand Up @@ -1577,8 +1583,8 @@ func (c *creator) insertLastID(ctx context.Context, insert *sql.InsertBuilder) e

// insertLastIDs invokes the batch insert query on the transaction and returns the LastInsertID of all entities.
func (c *batchCreator) insertLastIDs(ctx context.Context, tx dialect.ExecQuerier, insert *sql.InsertBuilder) error {
query, args := insert.Query()
if err := insert.Err(); err != nil {
query, args, err := insert.QueryErr()
if err != nil {
return err
}
// MySQL does not support the "RETURNING" clause.
Expand Down
12 changes: 12 additions & 0 deletions entc/integration/ent/entql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions entc/integration/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 147 additions & 1 deletion entc/integration/ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions entc/integration/ent/schema/task.go
Expand Up @@ -9,6 +9,7 @@ import (

"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"

"entgo.io/ent/entc/integration/ent/schema/task"
)
Expand All @@ -33,5 +34,17 @@ func (Task) Fields() []ent.Field {
Default(time.Now).
Immutable().
Nillable(),
field.String("name").
Optional(),
field.String("owner").
Optional(),
}
}

// Indexes of the Task.
func (Task) Indexes() []ent.Index {
return []ent.Index{
index.Fields("name", "owner").
Unique(),
}
}

0 comments on commit 8022271

Please sign in to comment.