OnlyID is like Only, but returns the only Entity ID in the query. Returns a *NotSingularError when more than one Entity ID is found. Returns a *NotFoundError when no entities are found.
(ctx context.Context)
| 206 | // Returns a *NotSingularError when more than one Entity ID is found. |
| 207 | // Returns a *NotFoundError when no entities are found. |
| 208 | func (eq *EntityQuery) OnlyID(ctx context.Context) (id int, err error) { |
| 209 | var ids []int |
| 210 | if ids, err = eq.Limit(2).IDs(setContextOp(ctx, eq.ctx, "OnlyID")); err != nil { |
| 211 | return |
| 212 | } |
| 213 | switch len(ids) { |
| 214 | case 1: |
| 215 | id = ids[0] |
| 216 | case 0: |
| 217 | err = &NotFoundError{entity.Label} |
| 218 | default: |
| 219 | err = &NotSingularError{entity.Label} |
| 220 | } |
| 221 | return |
| 222 | } |
| 223 | |
| 224 | // OnlyIDX is like OnlyID, but panics if an error occurs. |
| 225 | func (eq *EntityQuery) OnlyIDX(ctx context.Context) int { |
no test coverage detected