Budibase/budibase · error · HTTPError

Duplicate calculation on field "${schema.field}", calculatio

Error message

Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType} distinct"

What it means

Companion guard for distinct count aggregates: two COUNT ... distinct calculations on the same field are rejected. Regular duplicates are caught by guardDuplicateCalculationFields; this one covers the distinct-count case that the first guard deliberately skips.

Source

Thrown at packages/server/src/sdk/workspace/views/index.ts:141

  view: Omit<ViewV2, "id" | "version">
) {
  const seen: Record<string, Record<CalculationType, boolean>> = {}
  const calculationFields = helpers.views.calculationFields(view)
  for (const name of Object.keys(calculationFields)) {
    const schema = calculationFields[name]
    const isCount = schema.calculationType === CalculationType.COUNT
    if (!isCount) {
      continue
    }

    const isDistinct = "distinct" in schema
    if (!isDistinct) {
      // We do these separately.
      continue
    }

    if (seen[schema.field]?.[schema.calculationType]) {
      throw new HTTPError(
        `Duplicate calculation on field "${schema.field}", calculation type "${schema.calculationType} distinct"`,
        400
      )
    }
    seen[schema.field] ??= {} as Record<CalculationType, boolean>
    seen[schema.field][schema.calculationType] = true
  }
}

async function guardCalculationViewSchema(
  table: Table,
  view: Omit<ViewV2, "id" | "version">
) {
  const calculationFields = helpers.views.calculationFields(view)

  guardDuplicateCalculationFields(view)
  guardDuplicateCountDistinctFields(view)

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Keep at most one distinct COUNT per target field
  2. Drop the duplicate or point it at a different field
  3. Use a non-distinct COUNT alongside the distinct one if both are required

Example fix

// before
{ d1: { field: "email", calculationType: "count", distinct: true }, d2: { field: "email", calculationType: "count", distinct: true } }
// after
{ d1: { field: "email", calculationType: "count", distinct: true }, total: { field: "email", calculationType: "count" } }
Defensive patterns

Strategy: validation

Validate before calling

const seen = new Set()
for (const c of Object.values(view.calculations ?? {})) {
  if (c.calculationType === "count" && "distinct" in c) {
    if (seen.has(c.field)) throw new Error(`Duplicate distinct count on ${c.field}`)
    seen.add(c.field)
  }
}

Try / catch

try { await sdk.views.create(tableId, view) }
catch (e) {
  if (e instanceof HTTPError && e.status === 400 && /distinct/.test(e.message)) {
    // drop duplicate distinct count entries then retry
  } else throw e
}

Prevention

When it happens

Trigger: A calculation view schema containing two entries where schema.calculationType is COUNT, "distinct" in schema is true, and both target the same schema.field.

Common situations: Duplicated distinct-count columns in the builder; generated view JSON repeating a countDistinct field key under two names.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/a3d56437fbc90247. Report an issue: GitHub.