Only returns a single Machine entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Machine entity is found. Returns a *NotFoundError when no Machine entities are found.
(ctx context.Context)
| 132 | // Returns a *NotSingularError when more than one Machine entity is found. |
| 133 | // Returns a *NotFoundError when no Machine entities are found. |
| 134 | func (_q *MachineQuery) Only(ctx context.Context) (*Machine, error) { |
| 135 | nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) |
| 136 | if err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | switch len(nodes) { |
| 140 | case 1: |
| 141 | return nodes[0], nil |
| 142 | case 0: |
| 143 | return nil, &NotFoundError{machine.Label} |
| 144 | default: |
| 145 | return nil, &NotSingularError{machine.Label} |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // OnlyX is like Only, but panics if an error occurs. |
| 150 | func (_q *MachineQuery) OnlyX(ctx context.Context) *Machine { |
no test coverage detected