Part 4 of 6 · 2 min read
Schema-on-read versus schema-on-write
A document database typically enforces no shape on a document at write time; a relational table enforces its column shape on every row.
Think of a drawer where anyone can file any shape of paper. No clerk checks it first. Compare that to a form at a counter. A clerk refuses to accept it unless every required box is already filled in correctly.
In plain words: most document databases enforce no fixed shape on a document when it is written. This is called schema-on-read. The structure is only assumed, implicitly, by whatever code later reads the document. The database itself never checks it. A relational table does the opposite. This is schema-on-write. The database checks every row against the table's declared columns and types before storing it. Anything that does not fit is rejected. Schema-on-read suits data whose shape genuinely varies or changes often. Old and new document shapes can coexist without a migration. Schema-on-write suits data expected to always have the same shape. It catches a malformed record immediately, rather than letting it slip in silently.
A worked example. Say the bookstore splits a customer's single full_name field into first_name and last_name. A document database lets new customer documents simply include both new fields. Old documents keep only full_name. A small check in the application's read path handles the difference. A relational customers table instead requires an explicit ALTER TABLE to add the new columns. After that, every row, old and new, has the same fixed shape going forward.
The key lines.
-- Schema-on-write: the table itself enforces this shape on every row
ALTER TABLE customers ADD COLUMN first_name TEXT;
UPDATE customers SET first_name = split_part(full_name, ' ', 1);
// Schema-on-read: both shapes can coexist; the application decides how to read each
{"customer_id": 501, "full_name": "Priya Nair"}
{"customer_id": 502, "first_name": "Arjun", "last_name": "Mehta"}
Schema-on-read, schema-on-write
Schema-on-write is a database enforcing a fixed, declared shape on every row before it is stored. Anything that does not conform is rejected. This is the traditional relational approach. Schema-on-read stores data without enforcing any fixed shape. The structure is assumed by whatever code later reads it. This is the typical document-database default.
Example: a customers table requiring an ALTER TABLE before every row can have a first_name column is schema-on-write; a document collection letting old and new customer documents coexist with different fields is schema-on-read.