ent/ent · error

{{ $pkg }}: check existence: %w

Error message

{{ $pkg }}: check existence: %w

What it means

The generated query builder's Exist(ctx) method runs FirstID (or First) and maps the result: not-found means false, any other error is wrapped as "<pkg>: check existence: %w". The error itself just signals that the existence check could not complete — the real cause is in the wrapped error (connection failure, syntax error, driver issue, etc.).

Source

Thrown at entc/gen/template/builder/query.tmpl:276

}

// CountX is like Count, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) CountX(ctx context.Context) int {
	count, err := {{ $receiver }}.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func ({{ $receiver }} *{{ $builder }}) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, {{ $receiver }}.ctx, ent.OpQueryExist)
	switch _, err := {{ $receiver }}.First{{ if $.HasOneFieldID }}ID{{ end }}(ctx);{
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("{{ $pkg }}: check existence: %w", err)
	default:
		return true, nil
	}
}

// ExistX is like Exist, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) ExistX(ctx context.Context) bool {
	exist, err := {{ $receiver }}.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the {{ $builder }} builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func ({{ $receiver }} *{{ $builder }}) Clone() *{{ $builder }} {
	if {{ $receiver }} == nil {

View on GitHub (pinned to 69d5d4deb1)

Solutions

  1. Unwrap the returned error to see the root cause (errors.Unwrap / %w chain).
  2. Check database connectivity and that the table/schema exists (run migrations).
  3. Inspect the query's predicates for invalid values or types.
  4. Add timeouts/retry for transient connection errors.

Example fix

// before
exists, err := q.Exist(ctx)
// after
exists, err := q.Exist(ctx)
if err != nil {
    if ent.IsNotFound(err) { /* not possible for Exist, but unwrap anyway */ }
    log.Printf("existence check failed: %v", err)
}
Defensive patterns

Strategy: try-catch

Try / catch

exists, err := q.Exist(ctx)
if err != nil {
    // err is wrapped cause; inspect root
    if ctx.Err() != nil { return ctx.Err() }
    return fmt.Errorf("existence check: %w", err)
}
if exists { /* ... */ }

Prevention

When it happens

Trigger: Calling client.User.Query().Where(...).Exist(ctx) and the underlying FirstID/First query fails for any reason other than entc.NotFound — e.g. DB is down, invalid predicate produces bad SQL, context canceled.

Common situations: Database connection dropped mid-request; misconfigured predicates causing driver errors; context deadline exceeded on slow queries; migrations missing so a queried column/table doesn't exist.

Related errors


AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03). Data as JSON: /api/errors/f1f78d91a43d44d0. Report an issue: GitHub.