Only returns a single User entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one User entity is found. Returns a *NotFoundError when no User entities are found.
(ctx context.Context)
| 159 | // Returns a *NotSingularError when more than one User entity is found. |
| 160 | // Returns a *NotFoundError when no User entities are found. |
| 161 | func (_q *UserQuery) Only(ctx context.Context) (*User, error) { |
| 162 | nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly)) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | switch len(nodes) { |
| 167 | case 1: |
| 168 | return nodes[0], nil |
| 169 | case 0: |
| 170 | return nil, &NotFoundError{user.Label} |
| 171 | default: |
| 172 | return nil, &NotSingularError{user.Label} |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // OnlyX is like Only, but panics if an error occurs. |
| 177 | func (_q *UserQuery) OnlyX(ctx context.Context) *User { |
no test coverage detected