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 Card struct {
Number string `json:"number,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the CardQuery when eager-loading is set.
Edges CardEdges `json:"edges"`
owner_id *int
Edges CardEdges `json:"edges"`
user_card *int
}
// CardEdges holds the relations/edges for other nodes in the graph.
@@ -66,7 +66,7 @@ func (*Card) scanValues() []interface{} {
// fkValues returns the types for scanning foreign-keys values from sql.Rows.
func (*Card) fkValues() []interface{} {
return []interface{}{
&sql.NullInt64{}, // owner_id
&sql.NullInt64{}, // user_card
}
}
@@ -95,10 +95,10 @@ func (c *Card) assignValues(values ...interface{}) error {
values = values[2:]
if len(values) == len(card.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_card", value)
} else if value.Valid {
c.owner_id = new(int)
*c.owner_id = int(value.Int64)
c.user_card = new(int)
*c.user_card = 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_card"
)
// Columns holds all SQL columns for card fields.
@@ -36,5 +36,5 @@ var Columns = []string{
// ForeignKeys holds the SQL foreign-keys that are owned by the Card type.
var ForeignKeys = []string{
"owner_id",
"user_card",
}

View File

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

View File

@@ -17,7 +17,7 @@ var (
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "expired", Type: field.TypeTime},
{Name: "number", Type: field.TypeString},
{Name: "owner_id", Type: field.TypeInt, Unique: true, Nullable: true},
{Name: "user_card", Type: field.TypeInt, Unique: true, Nullable: true},
}
// CardsTable holds the schema information for the "cards" table.
CardsTable = &schema.Table{

View File

@@ -24,7 +24,7 @@ const (
// It exists in this package in order to avoid circular dependency with the "card" package.
CardInverseTable = "cards"
// CardColumn is the table column denoting the card relation/edge.
CardColumn = "owner_id"
CardColumn = "user_card"
)
// 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_card
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_card" 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_card" returned %v for node %v`, *fk, n.ID)
}
node.Edges.Card = n
}