Chapter 15 · Semi-Structured and Polymorphic Data

Schema-on-Write Versus Flexible Attributes

Compare schema-on-write with flexible attribute models, understand validation ownership, discoverability, evolution, and when flexibility helps or harms maintainability.

Beginner65–95 minutesSchema strategy + flexible attributesLast reviewed: August 2026

Learning outcomes

Schema-on-write validates structure before or during persistence. Flexible attribute designs delay some structural decisions and allow records of the same broad type to carry different attributes. Neither approach is universally superior. The right choice depends on stability, governance, queryability, and the cost of invalid data.

01

Compare strict and flexible schema strategies.

02

Understand where validation ownership moves when schemas become flexible.

03

Design metadata-driven attributes without losing discoverability.

04

Choose flexibility based on domain volatility rather than fashion.

Schema-on-write

sql · example
Part(  part_id BIGINT PRIMARY KEY,  sku TEXT NOT NULL UNIQUE,  description TEXT NOT NULL,  manufacturer_id BIGINT NOT NULL REFERENCES Manufacturer)

Data must satisfy the declared structure before it becomes persistent source-of-truth state.

Benefits of strict schemas

  • clear domains;
  • predictable queries;
  • declarative constraints;
  • discoverable metadata;
  • stable application contracts;
  • better optimizer statistics.

Cost of strict schemas

Adding new fields may require:

  • migration;
  • application changes;
  • backfill/default strategy;
  • compatibility planning.

Flexible attributes

One pattern:

model · example
Part(  part_id,  sku,  description,  attributes_json)

Different Part families can store different optional properties.

Flexibility moves responsibility

When the table no longer declares every field, someone else must define:

  • allowed names;
  • types;
  • required/optional status;
  • units;
  • validation;
  • display rules.
Flexibility is not absence of schema

It often means the schema has moved from DDL into metadata or application code.

Metadata-driven attribute definitions

model · example
AttributeDefinition(  attribute_code,  display_name,  data_type,  unit_code,  required_flag)

This can make flexible attributes discoverable and governed.

Per-category attributes

model · example
CategoryAttribute(  category_id,  attribute_code,  required_flag,  sort_order)

Now the system can say pumps require max_pressure while cables require conductor_count.

Where values live

Options include:

  • JSON document;
  • EAV rows;
  • subtype tables;
  • wide sparse table;
  • separate document store.

Lesson 4 examines EAV in depth.

Discoverability

A database analyst can inspect normal columns easily. Flexible metadata requires documentation and tooling so users know which attributes exist and how to query them.

Query stability

Strict column:

model · example
WHERE max_pressure_bar >= 20

Flexible JSON:

model · example
WHERE (attributes_json->>'max_pressure_bar')::numeric >= 20

The latter needs stable path/type assumptions and often specialized indexing.

Unit consistency

Flexible attributes can become dangerous if one record stores:

model · example
"pressure": 20   -- bar?

and another:

model · example
"pressure": 290  -- psi?

Attribute metadata must define units or values must carry them explicitly.

Promote maturing attributes

An attribute that starts experimental may later become:

  • required for most rows;
  • frequently filtered;
  • integrity-critical;
  • used in joins.

At that point, promote it into a normal column or related entity.

Schema evolution still exists

Flexible storage avoids some DDL migrations but introduces semantic migrations:

model · example
"temperature" -> "temperature_c"string -> numericlegacy code -> standardized code

Old documents must remain interpretable.

Validation versions

model · example
schema_version = 5

lets new validators coexist with historical records.

WorkshopHub strategy

Field familyStrategy
Core WorkOrder identifiersStrict relational columns
Service-type-specific questionnaireVersioned flexible schema
Manufacturer-specific metadataFlexible attributes initially
Common searchable technical ratingPromoted typed column

Practice: strict or flexible?

Inspection forms

WorkshopHub supports 80 equipment types, each with different inspection questions that change quarterly. Should every answer be a new WorkOrder column?

Review answer

No. A versioned metadata-driven form plus flexible answer payload is more appropriate. However, fields used for hard compliance rules or frequent cross-record analytics may deserve promoted relational columns or dedicated result tables.

Summary and next lesson

Schema-on-write gives strong contracts and queryability; flexible attributes reduce friction when shapes vary. Flexibility shifts rather than removes schema responsibility. The next lesson examines polymorphic associations, another common attempt to make schemas more generic.

References

  • Martin Kleppmann, Designing Data-Intensive Applications.
  • Bill Karwin, SQL Antipatterns.
  • DBMS documentation for JSON and generated columns.

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only assets compatible with the Ethereum/ERC-20 network. Do not send TRC-20/TRON assets.