doc/edges: add erds to examples (#3268)

This commit is contained in:
Ariel Mashraki
2023-01-23 15:53:33 +02:00
committed by GitHub
parent c9445d2794
commit 307eb5e30b
20 changed files with 527 additions and 170 deletions

View File

@@ -16,7 +16,7 @@ var (
NodesColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "value", Type: field.TypeInt},
{Name: "node_children", Type: field.TypeInt, Nullable: true},
{Name: "parent_id", Type: field.TypeInt, Nullable: true},
}
// NodesTable holds the schema information for the "nodes" table.
NodesTable = &schema.Table{

View File

@@ -204,9 +204,53 @@ func (m *NodeMutation) ResetValue() {
m.addvalue = nil
}
// SetParentID sets the "parent" edge to the Node entity by id.
func (m *NodeMutation) SetParentID(id int) {
m.parent = &id
// SetParentID sets the "parent_id" field.
func (m *NodeMutation) SetParentID(i int) {
m.parent = &i
}
// ParentID returns the value of the "parent_id" field in the mutation.
func (m *NodeMutation) ParentID() (r int, exists bool) {
v := m.parent
if v == nil {
return
}
return *v, true
}
// OldParentID returns the old "parent_id" field's value of the Node entity.
// If the Node object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *NodeMutation) OldParentID(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldParentID is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldParentID requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldParentID: %w", err)
}
return oldValue.ParentID, nil
}
// ClearParentID clears the value of the "parent_id" field.
func (m *NodeMutation) ClearParentID() {
m.parent = nil
m.clearedFields[node.FieldParentID] = struct{}{}
}
// ParentIDCleared returns if the "parent_id" field was cleared in this mutation.
func (m *NodeMutation) ParentIDCleared() bool {
_, ok := m.clearedFields[node.FieldParentID]
return ok
}
// ResetParentID resets all changes to the "parent_id" field.
func (m *NodeMutation) ResetParentID() {
m.parent = nil
delete(m.clearedFields, node.FieldParentID)
}
// ClearParent clears the "parent" edge to the Node entity.
@@ -216,15 +260,7 @@ func (m *NodeMutation) ClearParent() {
// ParentCleared reports if the "parent" edge to the Node entity was cleared.
func (m *NodeMutation) ParentCleared() bool {
return m.clearedparent
}
// ParentID returns the "parent" edge ID in the mutation.
func (m *NodeMutation) ParentID() (id int, exists bool) {
if m.parent != nil {
return *m.parent, true
}
return
return m.ParentIDCleared() || m.clearedparent
}
// ParentIDs returns the "parent" edge IDs in the mutation.
@@ -331,10 +367,13 @@ func (m *NodeMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *NodeMutation) Fields() []string {
fields := make([]string, 0, 1)
fields := make([]string, 0, 2)
if m.value != nil {
fields = append(fields, node.FieldValue)
}
if m.parent != nil {
fields = append(fields, node.FieldParentID)
}
return fields
}
@@ -345,6 +384,8 @@ func (m *NodeMutation) Field(name string) (ent.Value, bool) {
switch name {
case node.FieldValue:
return m.Value()
case node.FieldParentID:
return m.ParentID()
}
return nil, false
}
@@ -356,6 +397,8 @@ func (m *NodeMutation) OldField(ctx context.Context, name string) (ent.Value, er
switch name {
case node.FieldValue:
return m.OldValue(ctx)
case node.FieldParentID:
return m.OldParentID(ctx)
}
return nil, fmt.Errorf("unknown Node field %s", name)
}
@@ -372,6 +415,13 @@ func (m *NodeMutation) SetField(name string, value ent.Value) error {
}
m.SetValue(v)
return nil
case node.FieldParentID:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetParentID(v)
return nil
}
return fmt.Errorf("unknown Node field %s", name)
}
@@ -416,7 +466,11 @@ func (m *NodeMutation) AddField(name string, value ent.Value) error {
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *NodeMutation) ClearedFields() []string {
return nil
var fields []string
if m.FieldCleared(node.FieldParentID) {
fields = append(fields, node.FieldParentID)
}
return fields
}
// FieldCleared returns a boolean indicating if a field with the given name was
@@ -429,6 +483,11 @@ func (m *NodeMutation) FieldCleared(name string) bool {
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *NodeMutation) ClearField(name string) error {
switch name {
case node.FieldParentID:
m.ClearParentID()
return nil
}
return fmt.Errorf("unknown Node nullable field %s", name)
}
@@ -439,6 +498,9 @@ func (m *NodeMutation) ResetField(name string) error {
case node.FieldValue:
m.ResetValue()
return nil
case node.FieldParentID:
m.ResetParentID()
return nil
}
return fmt.Errorf("unknown Node field %s", name)
}

View File

@@ -21,10 +21,11 @@ type Node struct {
ID int `json:"id,omitempty"`
// Value holds the value of the "value" field.
Value int `json:"value,omitempty"`
// ParentID holds the value of the "parent_id" field.
ParentID int `json:"parent_id,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the NodeQuery when eager-loading is set.
Edges NodeEdges `json:"edges"`
node_children *int
Edges NodeEdges `json:"edges"`
}
// NodeEdges holds the relations/edges for other nodes in the graph.
@@ -65,9 +66,7 @@ func (*Node) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case node.FieldID, node.FieldValue:
values[i] = new(sql.NullInt64)
case node.ForeignKeys[0]: // node_children
case node.FieldID, node.FieldValue, node.FieldParentID:
values[i] = new(sql.NullInt64)
default:
return nil, fmt.Errorf("unexpected column %q for type Node", columns[i])
@@ -96,12 +95,11 @@ func (n *Node) assignValues(columns []string, values []any) error {
} else if value.Valid {
n.Value = int(value.Int64)
}
case node.ForeignKeys[0]:
case node.FieldParentID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for edge-field node_children", value)
return fmt.Errorf("unexpected type %T for field parent_id", values[i])
} else if value.Valid {
n.node_children = new(int)
*n.node_children = int(value.Int64)
n.ParentID = int(value.Int64)
}
}
}
@@ -143,6 +141,9 @@ func (n *Node) String() string {
builder.WriteString(fmt.Sprintf("id=%v, ", n.ID))
builder.WriteString("value=")
builder.WriteString(fmt.Sprintf("%v", n.Value))
builder.WriteString(", ")
builder.WriteString("parent_id=")
builder.WriteString(fmt.Sprintf("%v", n.ParentID))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -13,6 +13,8 @@ const (
FieldID = "id"
// FieldValue holds the string denoting the value field in the database.
FieldValue = "value"
// FieldParentID holds the string denoting the parent_id field in the database.
FieldParentID = "parent_id"
// EdgeParent holds the string denoting the parent edge name in mutations.
EdgeParent = "parent"
// EdgeChildren holds the string denoting the children edge name in mutations.
@@ -22,23 +24,18 @@ const (
// ParentTable is the table that holds the parent relation/edge.
ParentTable = "nodes"
// ParentColumn is the table column denoting the parent relation/edge.
ParentColumn = "node_children"
ParentColumn = "parent_id"
// ChildrenTable is the table that holds the children relation/edge.
ChildrenTable = "nodes"
// ChildrenColumn is the table column denoting the children relation/edge.
ChildrenColumn = "node_children"
ChildrenColumn = "parent_id"
)
// Columns holds all SQL columns for node fields.
var Columns = []string{
FieldID,
FieldValue,
}
// ForeignKeys holds the SQL foreign-keys that are owned by the "nodes"
// table and are not defined as standalone fields in the schema.
var ForeignKeys = []string{
"node_children",
FieldParentID,
}
// ValidColumn reports if the column name is valid (part of the table columns).
@@ -48,10 +45,5 @@ func ValidColumn(column string) bool {
return true
}
}
for i := range ForeignKeys {
if column == ForeignKeys[i] {
return true
}
}
return false
}

View File

@@ -62,6 +62,11 @@ func Value(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldValue, v))
}
// ParentID applies equality check predicate on the "parent_id" field. It's identical to ParentIDEQ.
func ParentID(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldParentID, v))
}
// ValueEQ applies the EQ predicate on the "value" field.
func ValueEQ(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldValue, v))
@@ -102,6 +107,36 @@ func ValueLTE(v int) predicate.Node {
return predicate.Node(sql.FieldLTE(FieldValue, v))
}
// ParentIDEQ applies the EQ predicate on the "parent_id" field.
func ParentIDEQ(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldParentID, v))
}
// ParentIDNEQ applies the NEQ predicate on the "parent_id" field.
func ParentIDNEQ(v int) predicate.Node {
return predicate.Node(sql.FieldNEQ(FieldParentID, v))
}
// ParentIDIn applies the In predicate on the "parent_id" field.
func ParentIDIn(vs ...int) predicate.Node {
return predicate.Node(sql.FieldIn(FieldParentID, vs...))
}
// ParentIDNotIn applies the NotIn predicate on the "parent_id" field.
func ParentIDNotIn(vs ...int) predicate.Node {
return predicate.Node(sql.FieldNotIn(FieldParentID, vs...))
}
// ParentIDIsNil applies the IsNil predicate on the "parent_id" field.
func ParentIDIsNil() predicate.Node {
return predicate.Node(sql.FieldIsNull(FieldParentID))
}
// ParentIDNotNil applies the NotNil predicate on the "parent_id" field.
func ParentIDNotNil() predicate.Node {
return predicate.Node(sql.FieldNotNull(FieldParentID))
}
// HasParent applies the HasEdge predicate on the "parent" edge.
func HasParent() predicate.Node {
return predicate.Node(func(s *sql.Selector) {

View File

@@ -29,16 +29,16 @@ func (nc *NodeCreate) SetValue(i int) *NodeCreate {
return nc
}
// SetParentID sets the "parent" edge to the Node entity by ID.
func (nc *NodeCreate) SetParentID(id int) *NodeCreate {
nc.mutation.SetParentID(id)
// SetParentID sets the "parent_id" field.
func (nc *NodeCreate) SetParentID(i int) *NodeCreate {
nc.mutation.SetParentID(i)
return nc
}
// SetNillableParentID sets the "parent" edge to the Node entity by ID if the given value is not nil.
func (nc *NodeCreate) SetNillableParentID(id *int) *NodeCreate {
if id != nil {
nc = nc.SetParentID(*id)
// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (nc *NodeCreate) SetNillableParentID(i *int) *NodeCreate {
if i != nil {
nc.SetParentID(*i)
}
return nc
}
@@ -153,7 +153,7 @@ func (nc *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) {
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.node_children = &nodes[0]
_node.ParentID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := nc.mutation.ChildrenIDs(); len(nodes) > 0 {

View File

@@ -28,7 +28,6 @@ type NodeQuery struct {
predicates []predicate.Node
withParent *NodeQuery
withChildren *NodeQuery
withFKs bool
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
@@ -406,19 +405,12 @@ func (nq *NodeQuery) prepareQuery(ctx context.Context) error {
func (nq *NodeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Node, error) {
var (
nodes = []*Node{}
withFKs = nq.withFKs
_spec = nq.querySpec()
loadedTypes = [2]bool{
nq.withParent != nil,
nq.withChildren != nil,
}
)
if nq.withParent != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, node.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*Node).scanValues(nil, columns)
}
@@ -457,10 +449,7 @@ func (nq *NodeQuery) loadParent(ctx context.Context, query *NodeQuery, nodes []*
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*Node)
for i := range nodes {
if nodes[i].node_children == nil {
continue
}
fk := *nodes[i].node_children
fk := nodes[i].ParentID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
@@ -477,7 +466,7 @@ func (nq *NodeQuery) loadParent(ctx context.Context, query *NodeQuery, nodes []*
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return fmt.Errorf(`unexpected foreign-key "node_children" returned %v`, n.ID)
return fmt.Errorf(`unexpected foreign-key "parent_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
@@ -495,7 +484,6 @@ func (nq *NodeQuery) loadChildren(ctx context.Context, query *NodeQuery, nodes [
init(nodes[i])
}
}
query.withFKs = true
query.Where(predicate.Node(func(s *sql.Selector) {
s.Where(sql.InValues(node.ChildrenColumn, fks...))
}))
@@ -504,13 +492,10 @@ func (nq *NodeQuery) loadChildren(ctx context.Context, query *NodeQuery, nodes [
return err
}
for _, n := range neighbors {
fk := n.node_children
if fk == nil {
return fmt.Errorf(`foreign-key "node_children" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
fk := n.ParentID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected foreign-key "node_children" returned %v for node %v`, *fk, n.ID)
return fmt.Errorf(`unexpected foreign-key "parent_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}

View File

@@ -44,20 +44,26 @@ func (nu *NodeUpdate) AddValue(i int) *NodeUpdate {
return nu
}
// SetParentID sets the "parent" edge to the Node entity by ID.
func (nu *NodeUpdate) SetParentID(id int) *NodeUpdate {
nu.mutation.SetParentID(id)
// SetParentID sets the "parent_id" field.
func (nu *NodeUpdate) SetParentID(i int) *NodeUpdate {
nu.mutation.SetParentID(i)
return nu
}
// SetNillableParentID sets the "parent" edge to the Node entity by ID if the given value is not nil.
func (nu *NodeUpdate) SetNillableParentID(id *int) *NodeUpdate {
if id != nil {
nu = nu.SetParentID(*id)
// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (nu *NodeUpdate) SetNillableParentID(i *int) *NodeUpdate {
if i != nil {
nu.SetParentID(*i)
}
return nu
}
// ClearParentID clears the value of the "parent_id" field.
func (nu *NodeUpdate) ClearParentID() *NodeUpdate {
nu.mutation.ClearParentID()
return nu
}
// SetParent sets the "parent" edge to the Node entity.
func (nu *NodeUpdate) SetParent(n *Node) *NodeUpdate {
return nu.SetParentID(n.ID)
@@ -283,20 +289,26 @@ func (nuo *NodeUpdateOne) AddValue(i int) *NodeUpdateOne {
return nuo
}
// SetParentID sets the "parent" edge to the Node entity by ID.
func (nuo *NodeUpdateOne) SetParentID(id int) *NodeUpdateOne {
nuo.mutation.SetParentID(id)
// SetParentID sets the "parent_id" field.
func (nuo *NodeUpdateOne) SetParentID(i int) *NodeUpdateOne {
nuo.mutation.SetParentID(i)
return nuo
}
// SetNillableParentID sets the "parent" edge to the Node entity by ID if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableParentID(id *int) *NodeUpdateOne {
if id != nil {
nuo = nuo.SetParentID(*id)
// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableParentID(i *int) *NodeUpdateOne {
if i != nil {
nuo.SetParentID(*i)
}
return nuo
}
// ClearParentID clears the value of the "parent_id" field.
func (nuo *NodeUpdateOne) ClearParentID() *NodeUpdateOne {
nuo.mutation.ClearParentID()
return nuo
}
// SetParent sets the "parent" edge to the Node entity.
func (nuo *NodeUpdateOne) SetParent(n *Node) *NodeUpdateOne {
return nuo.SetParentID(n.ID)

View File

@@ -19,6 +19,8 @@ type Node struct {
func (Node) Fields() []ent.Field {
return []ent.Field{
field.Int("value"),
field.Int("parent_id").
Optional(),
}
}
@@ -27,6 +29,7 @@ func (Node) Edges() []ent.Edge {
return []ent.Edge{
edge.To("children", Node.Type).
From("parent").
Field("parent_id").
Unique(),
}
}

View File

@@ -32,7 +32,7 @@ func Example_O2MRecur() {
// Output:
// Tree leafs [1 3 5]
// [1 3 5]
// Node(id=1, value=2)
// Node(id=1, value=2, parent_id=0)
}
func Do(ctx context.Context, client *ent.Client) error {
@@ -96,7 +96,7 @@ func Do(ctx context.Context, client *ent.Client) error {
Where(node.Not(node.HasParent())).
OnlyX(ctx)
fmt.Println(orphan)
// Output: Node(id=1, value=2)
// Output: Node(id=1, value=2, parent_id=0)
return nil
}

View File

@@ -16,7 +16,7 @@ var (
NodesColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "value", Type: field.TypeInt},
{Name: "node_next", Type: field.TypeInt, Unique: true, Nullable: true},
{Name: "prev_id", Type: field.TypeInt, Unique: true, Nullable: true},
}
// NodesTable holds the schema information for the "nodes" table.
NodesTable = &schema.Table{

View File

@@ -203,9 +203,53 @@ func (m *NodeMutation) ResetValue() {
m.addvalue = nil
}
// SetPrevID sets the "prev" edge to the Node entity by id.
func (m *NodeMutation) SetPrevID(id int) {
m.prev = &id
// SetPrevID sets the "prev_id" field.
func (m *NodeMutation) SetPrevID(i int) {
m.prev = &i
}
// PrevID returns the value of the "prev_id" field in the mutation.
func (m *NodeMutation) PrevID() (r int, exists bool) {
v := m.prev
if v == nil {
return
}
return *v, true
}
// OldPrevID returns the old "prev_id" field's value of the Node entity.
// If the Node object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *NodeMutation) OldPrevID(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldPrevID is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldPrevID requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldPrevID: %w", err)
}
return oldValue.PrevID, nil
}
// ClearPrevID clears the value of the "prev_id" field.
func (m *NodeMutation) ClearPrevID() {
m.prev = nil
m.clearedFields[node.FieldPrevID] = struct{}{}
}
// PrevIDCleared returns if the "prev_id" field was cleared in this mutation.
func (m *NodeMutation) PrevIDCleared() bool {
_, ok := m.clearedFields[node.FieldPrevID]
return ok
}
// ResetPrevID resets all changes to the "prev_id" field.
func (m *NodeMutation) ResetPrevID() {
m.prev = nil
delete(m.clearedFields, node.FieldPrevID)
}
// ClearPrev clears the "prev" edge to the Node entity.
@@ -215,15 +259,7 @@ func (m *NodeMutation) ClearPrev() {
// PrevCleared reports if the "prev" edge to the Node entity was cleared.
func (m *NodeMutation) PrevCleared() bool {
return m.clearedprev
}
// PrevID returns the "prev" edge ID in the mutation.
func (m *NodeMutation) PrevID() (id int, exists bool) {
if m.prev != nil {
return *m.prev, true
}
return
return m.PrevIDCleared() || m.clearedprev
}
// PrevIDs returns the "prev" edge IDs in the mutation.
@@ -315,10 +351,13 @@ func (m *NodeMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *NodeMutation) Fields() []string {
fields := make([]string, 0, 1)
fields := make([]string, 0, 2)
if m.value != nil {
fields = append(fields, node.FieldValue)
}
if m.prev != nil {
fields = append(fields, node.FieldPrevID)
}
return fields
}
@@ -329,6 +368,8 @@ func (m *NodeMutation) Field(name string) (ent.Value, bool) {
switch name {
case node.FieldValue:
return m.Value()
case node.FieldPrevID:
return m.PrevID()
}
return nil, false
}
@@ -340,6 +381,8 @@ func (m *NodeMutation) OldField(ctx context.Context, name string) (ent.Value, er
switch name {
case node.FieldValue:
return m.OldValue(ctx)
case node.FieldPrevID:
return m.OldPrevID(ctx)
}
return nil, fmt.Errorf("unknown Node field %s", name)
}
@@ -356,6 +399,13 @@ func (m *NodeMutation) SetField(name string, value ent.Value) error {
}
m.SetValue(v)
return nil
case node.FieldPrevID:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetPrevID(v)
return nil
}
return fmt.Errorf("unknown Node field %s", name)
}
@@ -400,7 +450,11 @@ func (m *NodeMutation) AddField(name string, value ent.Value) error {
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *NodeMutation) ClearedFields() []string {
return nil
var fields []string
if m.FieldCleared(node.FieldPrevID) {
fields = append(fields, node.FieldPrevID)
}
return fields
}
// FieldCleared returns a boolean indicating if a field with the given name was
@@ -413,6 +467,11 @@ func (m *NodeMutation) FieldCleared(name string) bool {
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *NodeMutation) ClearField(name string) error {
switch name {
case node.FieldPrevID:
m.ClearPrevID()
return nil
}
return fmt.Errorf("unknown Node nullable field %s", name)
}
@@ -423,6 +482,9 @@ func (m *NodeMutation) ResetField(name string) error {
case node.FieldValue:
m.ResetValue()
return nil
case node.FieldPrevID:
m.ResetPrevID()
return nil
}
return fmt.Errorf("unknown Node field %s", name)
}

View File

@@ -21,10 +21,11 @@ type Node struct {
ID int `json:"id,omitempty"`
// Value holds the value of the "value" field.
Value int `json:"value,omitempty"`
// PrevID holds the value of the "prev_id" field.
PrevID int `json:"prev_id,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the NodeQuery when eager-loading is set.
Edges NodeEdges `json:"edges"`
node_next *int
Edges NodeEdges `json:"edges"`
}
// NodeEdges holds the relations/edges for other nodes in the graph.
@@ -69,9 +70,7 @@ func (*Node) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case node.FieldID, node.FieldValue:
values[i] = new(sql.NullInt64)
case node.ForeignKeys[0]: // node_next
case node.FieldID, node.FieldValue, node.FieldPrevID:
values[i] = new(sql.NullInt64)
default:
return nil, fmt.Errorf("unexpected column %q for type Node", columns[i])
@@ -100,12 +99,11 @@ func (n *Node) assignValues(columns []string, values []any) error {
} else if value.Valid {
n.Value = int(value.Int64)
}
case node.ForeignKeys[0]:
case node.FieldPrevID:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for edge-field node_next", value)
return fmt.Errorf("unexpected type %T for field prev_id", values[i])
} else if value.Valid {
n.node_next = new(int)
*n.node_next = int(value.Int64)
n.PrevID = int(value.Int64)
}
}
}
@@ -147,6 +145,9 @@ func (n *Node) String() string {
builder.WriteString(fmt.Sprintf("id=%v, ", n.ID))
builder.WriteString("value=")
builder.WriteString(fmt.Sprintf("%v", n.Value))
builder.WriteString(", ")
builder.WriteString("prev_id=")
builder.WriteString(fmt.Sprintf("%v", n.PrevID))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -13,6 +13,8 @@ const (
FieldID = "id"
// FieldValue holds the string denoting the value field in the database.
FieldValue = "value"
// FieldPrevID holds the string denoting the prev_id field in the database.
FieldPrevID = "prev_id"
// EdgePrev holds the string denoting the prev edge name in mutations.
EdgePrev = "prev"
// EdgeNext holds the string denoting the next edge name in mutations.
@@ -22,23 +24,18 @@ const (
// PrevTable is the table that holds the prev relation/edge.
PrevTable = "nodes"
// PrevColumn is the table column denoting the prev relation/edge.
PrevColumn = "node_next"
PrevColumn = "prev_id"
// NextTable is the table that holds the next relation/edge.
NextTable = "nodes"
// NextColumn is the table column denoting the next relation/edge.
NextColumn = "node_next"
NextColumn = "prev_id"
)
// Columns holds all SQL columns for node fields.
var Columns = []string{
FieldID,
FieldValue,
}
// ForeignKeys holds the SQL foreign-keys that are owned by the "nodes"
// table and are not defined as standalone fields in the schema.
var ForeignKeys = []string{
"node_next",
FieldPrevID,
}
// ValidColumn reports if the column name is valid (part of the table columns).
@@ -48,10 +45,5 @@ func ValidColumn(column string) bool {
return true
}
}
for i := range ForeignKeys {
if column == ForeignKeys[i] {
return true
}
}
return false
}

View File

@@ -62,6 +62,11 @@ func Value(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldValue, v))
}
// PrevID applies equality check predicate on the "prev_id" field. It's identical to PrevIDEQ.
func PrevID(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldPrevID, v))
}
// ValueEQ applies the EQ predicate on the "value" field.
func ValueEQ(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldValue, v))
@@ -102,6 +107,36 @@ func ValueLTE(v int) predicate.Node {
return predicate.Node(sql.FieldLTE(FieldValue, v))
}
// PrevIDEQ applies the EQ predicate on the "prev_id" field.
func PrevIDEQ(v int) predicate.Node {
return predicate.Node(sql.FieldEQ(FieldPrevID, v))
}
// PrevIDNEQ applies the NEQ predicate on the "prev_id" field.
func PrevIDNEQ(v int) predicate.Node {
return predicate.Node(sql.FieldNEQ(FieldPrevID, v))
}
// PrevIDIn applies the In predicate on the "prev_id" field.
func PrevIDIn(vs ...int) predicate.Node {
return predicate.Node(sql.FieldIn(FieldPrevID, vs...))
}
// PrevIDNotIn applies the NotIn predicate on the "prev_id" field.
func PrevIDNotIn(vs ...int) predicate.Node {
return predicate.Node(sql.FieldNotIn(FieldPrevID, vs...))
}
// PrevIDIsNil applies the IsNil predicate on the "prev_id" field.
func PrevIDIsNil() predicate.Node {
return predicate.Node(sql.FieldIsNull(FieldPrevID))
}
// PrevIDNotNil applies the NotNil predicate on the "prev_id" field.
func PrevIDNotNil() predicate.Node {
return predicate.Node(sql.FieldNotNull(FieldPrevID))
}
// HasPrev applies the HasEdge predicate on the "prev" edge.
func HasPrev() predicate.Node {
return predicate.Node(func(s *sql.Selector) {

View File

@@ -29,16 +29,16 @@ func (nc *NodeCreate) SetValue(i int) *NodeCreate {
return nc
}
// SetPrevID sets the "prev" edge to the Node entity by ID.
func (nc *NodeCreate) SetPrevID(id int) *NodeCreate {
nc.mutation.SetPrevID(id)
// SetPrevID sets the "prev_id" field.
func (nc *NodeCreate) SetPrevID(i int) *NodeCreate {
nc.mutation.SetPrevID(i)
return nc
}
// SetNillablePrevID sets the "prev" edge to the Node entity by ID if the given value is not nil.
func (nc *NodeCreate) SetNillablePrevID(id *int) *NodeCreate {
if id != nil {
nc = nc.SetPrevID(*id)
// SetNillablePrevID sets the "prev_id" field if the given value is not nil.
func (nc *NodeCreate) SetNillablePrevID(i *int) *NodeCreate {
if i != nil {
nc.SetPrevID(*i)
}
return nc
}
@@ -157,7 +157,7 @@ func (nc *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) {
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.node_next = &nodes[0]
_node.PrevID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := nc.mutation.NextIDs(); len(nodes) > 0 {

View File

@@ -28,7 +28,6 @@ type NodeQuery struct {
predicates []predicate.Node
withPrev *NodeQuery
withNext *NodeQuery
withFKs bool
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
@@ -406,19 +405,12 @@ func (nq *NodeQuery) prepareQuery(ctx context.Context) error {
func (nq *NodeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Node, error) {
var (
nodes = []*Node{}
withFKs = nq.withFKs
_spec = nq.querySpec()
loadedTypes = [2]bool{
nq.withPrev != nil,
nq.withNext != nil,
}
)
if nq.withPrev != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, node.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*Node).scanValues(nil, columns)
}
@@ -456,10 +448,7 @@ func (nq *NodeQuery) loadPrev(ctx context.Context, query *NodeQuery, nodes []*No
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*Node)
for i := range nodes {
if nodes[i].node_next == nil {
continue
}
fk := *nodes[i].node_next
fk := nodes[i].PrevID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
@@ -476,7 +465,7 @@ func (nq *NodeQuery) loadPrev(ctx context.Context, query *NodeQuery, nodes []*No
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return fmt.Errorf(`unexpected foreign-key "node_next" returned %v`, n.ID)
return fmt.Errorf(`unexpected foreign-key "prev_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
@@ -491,7 +480,6 @@ func (nq *NodeQuery) loadNext(ctx context.Context, query *NodeQuery, nodes []*No
fks = append(fks, nodes[i].ID)
nodeids[nodes[i].ID] = nodes[i]
}
query.withFKs = true
query.Where(predicate.Node(func(s *sql.Selector) {
s.Where(sql.InValues(node.NextColumn, fks...))
}))
@@ -500,13 +488,10 @@ func (nq *NodeQuery) loadNext(ctx context.Context, query *NodeQuery, nodes []*No
return err
}
for _, n := range neighbors {
fk := n.node_next
if fk == nil {
return fmt.Errorf(`foreign-key "node_next" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
fk := n.PrevID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected foreign-key "node_next" returned %v for node %v`, *fk, n.ID)
return fmt.Errorf(`unexpected foreign-key "prev_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}

View File

@@ -44,20 +44,26 @@ func (nu *NodeUpdate) AddValue(i int) *NodeUpdate {
return nu
}
// SetPrevID sets the "prev" edge to the Node entity by ID.
func (nu *NodeUpdate) SetPrevID(id int) *NodeUpdate {
nu.mutation.SetPrevID(id)
// SetPrevID sets the "prev_id" field.
func (nu *NodeUpdate) SetPrevID(i int) *NodeUpdate {
nu.mutation.SetPrevID(i)
return nu
}
// SetNillablePrevID sets the "prev" edge to the Node entity by ID if the given value is not nil.
func (nu *NodeUpdate) SetNillablePrevID(id *int) *NodeUpdate {
if id != nil {
nu = nu.SetPrevID(*id)
// SetNillablePrevID sets the "prev_id" field if the given value is not nil.
func (nu *NodeUpdate) SetNillablePrevID(i *int) *NodeUpdate {
if i != nil {
nu.SetPrevID(*i)
}
return nu
}
// ClearPrevID clears the value of the "prev_id" field.
func (nu *NodeUpdate) ClearPrevID() *NodeUpdate {
nu.mutation.ClearPrevID()
return nu
}
// SetPrev sets the "prev" edge to the Node entity.
func (nu *NodeUpdate) SetPrev(n *Node) *NodeUpdate {
return nu.SetPrevID(n.ID)
@@ -253,20 +259,26 @@ func (nuo *NodeUpdateOne) AddValue(i int) *NodeUpdateOne {
return nuo
}
// SetPrevID sets the "prev" edge to the Node entity by ID.
func (nuo *NodeUpdateOne) SetPrevID(id int) *NodeUpdateOne {
nuo.mutation.SetPrevID(id)
// SetPrevID sets the "prev_id" field.
func (nuo *NodeUpdateOne) SetPrevID(i int) *NodeUpdateOne {
nuo.mutation.SetPrevID(i)
return nuo
}
// SetNillablePrevID sets the "prev" edge to the Node entity by ID if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillablePrevID(id *int) *NodeUpdateOne {
if id != nil {
nuo = nuo.SetPrevID(*id)
// SetNillablePrevID sets the "prev_id" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillablePrevID(i *int) *NodeUpdateOne {
if i != nil {
nuo.SetPrevID(*i)
}
return nuo
}
// ClearPrevID clears the value of the "prev_id" field.
func (nuo *NodeUpdateOne) ClearPrevID() *NodeUpdateOne {
nuo.mutation.ClearPrevID()
return nuo
}
// SetPrev sets the "prev" edge to the Node entity.
func (nuo *NodeUpdateOne) SetPrev(n *Node) *NodeUpdateOne {
return nuo.SetPrevID(n.ID)

View File

@@ -19,6 +19,8 @@ type Node struct {
func (Node) Fields() []ent.Field {
return []ent.Field{
field.Int("value"),
field.Int("prev_id").
Optional(),
}
}
@@ -28,6 +30,7 @@ func (Node) Edges() []ent.Edge {
edge.To("next", Node.Type).
Unique().
From("prev").
Field("prev_id").
Unique(),
}
}