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

@@ -16,7 +16,7 @@ var (
PetsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString},
{Name: "owner_id", Type: field.TypeInt, Nullable: true},
{Name: "user_pets", Type: field.TypeInt, Nullable: true},
}
// PetsTable holds the schema information for the "pets" table.
PetsTable = &schema.Table{

View File

@@ -24,8 +24,8 @@ type Pet struct {
Name string `json:"name,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the PetQuery when eager-loading is set.
Edges PetEdges `json:"edges"`
owner_id *int
Edges PetEdges `json:"edges"`
user_pets *int
}
// PetEdges holds the relations/edges for other nodes in the graph.
@@ -62,7 +62,7 @@ func (*Pet) scanValues() []interface{} {
// fkValues returns the types for scanning foreign-keys values from sql.Rows.
func (*Pet) fkValues() []interface{} {
return []interface{}{
&sql.NullInt64{}, // owner_id
&sql.NullInt64{}, // user_pets
}
}
@@ -86,10 +86,10 @@ func (pe *Pet) assignValues(values ...interface{}) error {
values = values[1:]
if len(values) == len(pet.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_pets", value)
} else if value.Valid {
pe.owner_id = new(int)
*pe.owner_id = int(value.Int64)
pe.user_pets = new(int)
*pe.user_pets = int(value.Int64)
}
}
return nil

View File

@@ -22,7 +22,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_pets"
)
// Columns holds all SQL columns for pet fields.
@@ -33,5 +33,5 @@ var Columns = []string{
// ForeignKeys holds the SQL foreign-keys that are owned by the Pet type.
var ForeignKeys = []string{
"owner_id",
"user_pets",
}

View File

@@ -335,7 +335,7 @@ func (pq *PetQuery) sqlAll(ctx context.Context) ([]*Pet, error) {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*Pet)
for i := range nodes {
if fk := nodes[i].owner_id; fk != nil {
if fk := nodes[i].user_pets; fk != nil {
ids = append(ids, *fk)
nodeids[*fk] = append(nodeids[*fk], nodes[i])
}
@@ -348,7 +348,7 @@ func (pq *PetQuery) sqlAll(ctx context.Context) ([]*Pet, 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_pets" returned %v`, n.ID)
}
for i := range nodes {
nodes[i].Edges.Owner = n

View File

@@ -24,7 +24,7 @@ const (
// It exists in this package in order to avoid circular dependency with the "pet" package.
PetsInverseTable = "pets"
// PetsColumn is the table column denoting the pets relation/edge.
PetsColumn = "owner_id"
PetsColumn = "user_pets"
)
// Columns holds all SQL columns for user fields.

View File

@@ -337,13 +337,13 @@ func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
return nil, err
}
for _, n := range neighbors {
fk := n.owner_id
fk := n.user_pets
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_pets" 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_pets" returned %v for node %v`, *fk, n.ID)
}
node.Edges.Pets = append(node.Edges.Pets, n)
}