PRACTICAL FIELD GUIDE

Nullable vs required columns: when to use NOT NULL

A nullable column and a required column encode different guarantees for every API caller. Decide how missing, empty and unknown values should behave before adding a not-null constraint.

MCPBackend editorial team · · Examples are illustrative

The short answer

Make a column required (NOT NULL) when every row must have a real value the moment it is created: an owner, a status, a title. Leave it nullable when the value can arrive later or never: a bio, a completed_at date, a phone number. If a value always exists but callers should not have to send it, make it required with a default.

In MCPBackend, columns are nullable unless you set notNull: true in create_table or add_column. A required column makes the database reject any insert that omits it or sends null: the API returns 400 bad_request with the SQLite message NOT NULL constraint failed: table.column. The generated OpenAPI contract lists required columns under required, so an agent writing your client code can see them.

How to decide, column by column

  • Required: identity and ownership (user_id), anything every screen relies on (title, status), and foreign keys to a parent row that must exist.
  • Required with a default: values that always exist but are set by the system, such as status with a default of draft or created_at with a default of CURRENT_TIMESTAMP.
  • Nullable: optional profile fields, values that appear later in a workflow (paid_at, completed_at), and data copied from integrations that may be missing.
  • Not a NOT NULL job: rejecting empty strings. NOT NULL accepts "", so check for blank text in your app or form validation.

Example table definition

The create_table MCP tool takes the same column fields the dashboard uses. Here title and status are required, status has a default, and the rest are optional:

{
  "name": "tasks",
  "columns": [
    { "name": "title",        "type": "text",      "notNull": true },
    { "name": "status",       "type": "text",      "notNull": true, "defaultValue": "open" },
    { "name": "notes",        "type": "text" },
    { "name": "due_date",     "type": "text" },
    { "name": "completed_at", "type": "timestamp" }
  ]
}
// POST { "title": "Ship" }                 -> 201, status = "open"
// POST { "notes": "no title" }             -> 400 NOT NULL constraint failed: tasks.title
// POST { "title": "Ship", "status": null } -> 400 (an explicit null does not fall back to the default)
// POST { "title": "" }                     -> 201 (empty text is not NULL)

Making a column required later

Adding a required column to an existing table needs a default, because existing rows need a value. add_column with notNull: true and no defaultValue is rejected with 400 needs_default (NOT NULL columns need a default value when added to an existing table). Give it a constant default such as an empty string, 0 or a status value.

There is no tool to switch an existing column between nullable and required in place. Decide when you create the table, or add a new column with a default, copy the data across and point the app at the new column.

Put it into practice

  1. List valid creation states and distinguish an absent value from an empty string.
  2. Select required fields and defaults that reflect the product workflow.
  3. Test omitted, null and empty values through the actual endpoint and document the accepted forms.

What this looks like

ILLUSTRATIVE EXAMPLE

A profile can exist before a user adds a biography. Requiring a biography at signup creates placeholder text rather than better data.

A boundary to keep clear

A not-null constraint does not reject an empty string. Content validation may still belong in application logic.

MCPBackend context

MCPBackend stores each project's data in SQLite and exposes supported tables through generated REST endpoints. Use the schema tools and dashboard for supported changes. The design examples here are patterns to adapt, not claims that the API exposes every SQLite feature or arbitrary SQL.

Take the next step

Inspect the current project contract, try the change with disposable data, and verify the result through the same credentials your app will use. Record the expected response and one denied-access case before release.

Frequently asked questions

Should database columns be nullable by default?

Only for values that can genuinely be missing. Make identity, ownership and state columns required, give system-set values a default, and leave optional or later-arriving values nullable.

What happens if I send null to a NOT NULL column?

The insert fails. MCPBackend returns 400 bad_request with the message NOT NULL constraint failed: table.column. A default only applies when the field is left out, not when it is sent as null.

Does NOT NULL stop empty strings?

No. An empty string is a value, so NOT NULL accepts it. Validate blank text in your app.

Why does add_column fail with needs_default?

Existing rows need a value for a new required column. Add a constant defaultValue, or add the column as nullable.

Sources and further reading

These references explain the underlying protocols and design principles. For supported MCPBackend operations and exact request shapes, inspect your project’s generated API contract.