{"record":{"id":"4e9416d47931ecfc","repo":"Budibase/budibase","slug":"cannot-make-field-key-required-it-has-a-defa","errorCode":null,"errorMessage":"Cannot make field \"${key}\" required, it has a default value.","messagePattern":"Cannot make field \"(.+?)\" required, it has a default value\\.","errorType":"http","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/api/controllers/table/index.ts","lineNumber":81,"sourceCode":"import { getRowParams } from \"../../../db/utils\"\n\nfunction pickApi({ tableId, table }: { tableId?: string; table?: Table }) {\n  if (table && isExternalTable(table)) {\n    return external\n  }\n  if (tableId && isExternalTableID(tableId)) {\n    return external\n  }\n  return internal\n}\n\nfunction checkDefaultFields(table: Table) {\n  for (const [key, field] of Object.entries(table.schema)) {\n    if (!(\"default\" in field) || field.default == null) {\n      continue\n    }\n    if (helpers.schema.isRequired(field.constraints)) {\n      throw new HTTPError(\n        `Cannot make field \"${key}\" required, it has a default value.`,\n        400\n      )\n    }\n  }\n}\n\nfunction stripIgnoreTimezoneSuffix(rows: Row[], table: Table): Row[] {\n  const columns = Object.entries(table.schema)\n    .filter(\n      ([_, schema]) =>\n        schema.type === FieldType.DATETIME &&\n        schema.ignoreTimezones &&\n        !schema.timeOnly\n    )\n    .map(([name]) => name)\n  if (!columns.length) {\n    return rows","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/api/controllers/table/index.ts#L63-L99","documentation":"Budibase forbids a table field from being both required and carrying a default value, because the semantics conflict (a default would satisfy validation, making required meaningless). checkDefaultFields runs on every table save via guardTable and returns HTTP 400 when a schema field has a non-null default and required constraints.","triggerScenarios":"Saving a table (POST/PUT to table API) whose schema contains a field with both a \"default\" value and constraints indicating required (helpers.schema.isRequired true).","commonSituations":"Builder UI or automation scripts marking a column required after a default was set; imported table definitions with both properties; API clients constructing schema JSON by hand.","solutions":["Remove the \"default\" key from the field schema if the field must be required","Drop the required constraint (set constraints.presence.required to false) if the default should be kept","Re-save through the Builder UI which enforces the mutual exclusion interactively"],"exampleFix":"// before\nschema: { status: { type: \"string\", constraints: { presence: { required: true } }, default: \"draft\" } }\n// after\nschema: { status: { type: \"string\", constraints: { presence: { required: true } } } }","handlingStrategy":"validation","validationCode":"function fieldHasConflict(field) {\n  const hasDefault = field != null && \"default\" in field && field.default != null\n  const isRequired = field?.constraints?.presence?.required === true\n  return hasDefault && isRequired\n}\nfor (const key of Object.keys(table.schema)) {\n  if (fieldHasConflict(table.schema[key])) throw new Error(`field ${key}: required + default conflict`)\n}","typeGuard":"const hasDefaultAndRequired = (f) => f != null && \"default\" in f && f.default != null && f?.constraints?.presence?.required === true","tryCatchPattern":"try {\n  await api.saveTable(table)\n} catch (e) {\n  if (e?.status === 400 && /required, it has a default value/.test(e.message)) {\n    const key = e.message.match(/field \\\"(.+?)\\\"/)?.[1]\n    // strip default or required for that key and retry\n  } else throw e\n}","preventionTips":["Enforce default-XOR-required in your schema builders","Validate table schema JSON before POST/PUT","Review imported table definitions for both properties","Use the Builder UI for schema edits where the rule is enforced"],"tags":["table","schema","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}