dialect/sql/sqlgraph: support sql.Scanner types when scanning IDs (#1987)

Fixed https://github.com/ent/ent/issues/1985
This commit is contained in:
Ariel Mashraki
2021-09-27 17:49:57 +03:00
committed by GitHub
parent 0c4e9c78a7
commit 4306643d16
21 changed files with 2903 additions and 25 deletions

View File

@@ -1300,17 +1300,23 @@ func (c *creator) insertLastID(ctx context.Context, insert *sql.InsertBuilder) e
return err
}
defer rows.Close()
if !c.ID.Type.Numeric() {
switch _, ok := c.ID.Value.(field.ValueScanner); {
case ok:
// If the ID implements the sql.Scanner
// interface it should be a pointer type.
return sql.ScanOne(rows, c.ID.Value)
case c.ID.Type.Numeric():
// Normalize the type to int64 to make it
// looks like LastInsertId.
id, err := sql.ScanInt64(rows)
if err != nil {
return err
}
c.ID.Value = id
return nil
default:
return sql.ScanOne(rows, &c.ID.Value)
}
// Normalize the type to int64 to make it looks
// like LastInsertId.
id, err := sql.ScanInt64(rows)
if err != nil {
return err
}
c.ID.Value = id
return nil
}
// MySQL.
var res sql.Result