Flexible attributes: typed columns, JSON and EAV
Overview
Use typed relational columns for stable, frequently constrained facts, such as item identifiers and SKU. JSON can hold sparse or evolving metadata; entity–attribute–value (EAV) stores attributes as individual rows. The fixture stores optional color and weight in valid JSON text and provides a normalized tag membership table. SQLite JSON functions inspect values inside the document.
First example
SELECT id,json_extract(attrs,'$.color'),json_type(attrs,'$.color') FROM pro_item ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 101, "blue", "text" ], [ 102, null, "null" ], [ 201, null, null ]]How it works
Example 1 extracts color and shows json_type: missing path returns SQL NULL, explicit JSON null returns the text null. Example 2 selects items with an exact normalized tag. JSON validity does not guarantee required fields or types; use explicit validation and promote frequently constrained values to typed columns. PostgreSQL jsonb has different operators and indexing from SQLite. EAV supports dynamic attributes but requires attribute definitions, type validation and often more joins; a single text value column makes numeric comparisons fragile. A polymorphic type/id reference lacks a normal FK to multiple target tables; use explicit association tables when integrity matters.
Practical example
SELECT item_id FROM pro_tag WHERE tag='office' ORDER BY item_id;Observed SQLite result (rows in JSON):
[ [ 101 ], [ 102 ]]Practice and mistakes
Practice: distinguish a missing weight from JSON null using json_type, then select only numeric weights. Decide whether weight should become a typed column if it is required for every shipment. Do not use LIKE over JSON text as a substitute for structural queries.