hasura/graphql-engine · error · ErrorWithStatusCode

Can't create alias for functions

Error message

Can't create alias for functions

What it means

generateTargetAlias produces a table alias for a target; functions are not tables and cannot be aliased, so a target of type 'function' triggers an ErrorWithStatusCode(500). This is reached from alias generation for joins, relationships, subqueries, and top-level targets.

Source

Thrown at dc-agents/sqlite/src/query.ts:364

): string {
  switch (comparisonValue.type) {
    case 'column':
      return generateComparisonColumnFragment(
        comparisonValue.column,
        queryTableAlias,
        currentTableAlias,
      );
    case 'scalar':
      return escapeString(comparisonValue.value);
    default:
      return unreachable(comparisonValue['type']);
  }
}

export function generateTargetAlias(target: Target): string {
  switch (target.type) {
    case 'function':
      throw new ErrorWithStatusCode("Can't create alias for functions", 500, {
        target,
      });
    case 'interpolated':
      return generateTableAlias([target.id]);
    case 'table':
      return generateTableAlias(target.name);
  }
}

function generateTableAlias(tableName: TableName): string {
  return generateIdentifierAlias(validateTableName(tableName).join('_'));
}

function generateIdentifierAlias(identifier: string): string {
  const randomSuffix = nanoid();
  return escapeIdentifier(`${identifier}_${randomSuffix}`);
}

View on GitHub (pinned to 724551b9ae)

Solutions

  1. Replace the function target with a table or interpolated target throughout the request tree
  2. Validate the whole request tree client-side before sending (walk every target and relationship target)
  3. Upgrade the agent so function targets fail earlier with the clearer 'not supported in queries' message

Example fix

// before
relationship: { target: { type: 'function', name: 'f' } }
// after
relationship: { target: { type: 'table', name: ['main','public','f_result'] } }
Defensive patterns

Strategy: type-guard

Validate before calling

if (target.type === 'function') throw new Error('Cannot alias function targets');

Type guard

const isAliasableTarget = (t: Target): t is TableTarget | InterpolatedTarget => t.type !== 'function';

Prevention

When it happens

Trigger: Any query tree where a function target flows into alias generation — top-level target, relationship target, or join target — without having been rejected earlier.

Common situations: Function targets embedded in relationship chains; client code that generically wraps every entity as a target; metadata drift after adding tracked functions.

Related errors


AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28). Data as JSON: /api/errors/64d4e558e0240f25. Report an issue: GitHub.