entc/gen: change name format for edge fks (#286)

* entc/gen: change name format for edge fks

* dialect/sql/schema: add fixture support for mysql and postgres

* sql/dialect: merge fkcolumn queries to one for the 2 dialects
This commit is contained in:
Ariel Mashraki
2020-02-03 15:41:55 +02:00
committed by GitHub
parent c70f1017e3
commit b4255998bf
113 changed files with 1082 additions and 431 deletions

View File

@@ -27,8 +27,8 @@ type Car struct {
RegisteredAt time.Time `json:"registered_at,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the CarQuery when eager-loading is set.
Edges CarEdges `json:"edges"`
owner_id *int
Edges CarEdges `json:"edges"`
user_cars *int
}
// CarEdges holds the relations/edges for other nodes in the graph.
@@ -66,7 +66,7 @@ func (*Car) scanValues() []interface{} {
// fkValues returns the types for scanning foreign-keys values from sql.Rows.
func (*Car) fkValues() []interface{} {
return []interface{}{
&sql.NullInt64{}, // owner_id
&sql.NullInt64{}, // user_cars
}
}
@@ -95,10 +95,10 @@ func (c *Car) assignValues(values ...interface{}) error {
values = values[2:]
if len(values) == len(car.ForeignKeys) {
if value, ok := values[0].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for edge-field owner_id", value)
return fmt.Errorf("unexpected type %T for edge-field user_cars", value)
} else if value.Valid {
c.owner_id = new(int)
*c.owner_id = int(value.Int64)
c.user_cars = new(int)
*c.user_cars = int(value.Int64)
}
}
return nil

View File

@@ -24,7 +24,7 @@ const (
// It exists in this package in order to avoid circular dependency with the "user" package.
OwnerInverseTable = "users"
// OwnerColumn is the table column denoting the owner relation/edge.
OwnerColumn = "owner_id"
OwnerColumn = "user_cars"
)
// Columns holds all SQL columns for car fields.
@@ -36,5 +36,5 @@ var Columns = []string{
// ForeignKeys holds the SQL foreign-keys that are owned by the Car type.
var ForeignKeys = []string{
"owner_id",
"user_cars",
}

View File

@@ -335,7 +335,7 @@ func (cq *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*Car)
for i := range nodes {
if fk := nodes[i].owner_id; fk != nil {
if fk := nodes[i].user_cars; fk != nil {
ids = append(ids, *fk)
nodeids[*fk] = append(nodeids[*fk], nodes[i])
}
@@ -348,7 +348,7 @@ func (cq *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return nil, fmt.Errorf(`unexpected foreign-key "owner_id" returned %v`, n.ID)
return nil, fmt.Errorf(`unexpected foreign-key "user_cars" returned %v`, n.ID)
}
for i := range nodes {
nodes[i].Edges.Owner = n

View File

@@ -19,7 +19,7 @@ var (
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "model", Type: field.TypeString},
{Name: "registered_at", Type: field.TypeTime},
{Name: "owner_id", Type: field.TypeInt, Nullable: true},
{Name: "user_cars", Type: field.TypeInt, Nullable: true},
}
// CarsTable holds the schema information for the "cars" table.
CarsTable = &schema.Table{

View File

@@ -28,7 +28,7 @@ const (
// It exists in this package in order to avoid circular dependency with the "car" package.
CarsInverseTable = "cars"
// CarsColumn is the table column denoting the cars relation/edge.
CarsColumn = "owner_id"
CarsColumn = "user_cars"
// GroupsTable is the table the holds the groups relation/edge. The primary key declared below.
GroupsTable = "group_users"
// GroupsInverseTable is the table name for the Group entity.

View File

@@ -363,13 +363,13 @@ func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
return nil, err
}
for _, n := range neighbors {
fk := n.owner_id
fk := n.user_cars
if fk == nil {
return nil, fmt.Errorf(`foreign-key "owner_id" is nil for node %v`, n.ID)
return nil, fmt.Errorf(`foreign-key "user_cars" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
if !ok {
return nil, fmt.Errorf(`unexpected foreign-key "owner_id" returned %v for node %v`, *fk, n.ID)
return nil, fmt.Errorf(`unexpected foreign-key "user_cars" returned %v for node %v`, *fk, n.ID)
}
node.Edges.Cars = append(node.Edges.Cars, n)
}