Topic 4 of 5 · 5 min read
Schema anti-patterns in generated models
Goal: Name the anti-pattern in an AI-generated schema and rewrite the table so the problem cannot occur.
Learn the handful of schema shapes that look reasonable, pass a demo and fail in production, because assistants produce them often. A model completes the most common pattern in its training data, and the common pattern is not always the sound one.
Check yourself · 1 of 3
A generated table has a column role_ids text holding values like '1,4,9'. What is the fix?
Why it matters
These mistakes are cheap to fix on day one and very expensive after a year of data. Each one also breaks quietly: wrong totals, rows that cannot be queried, times that shift by an hour.
The five to recognise
- Comma lists in one column.
tags = 'red,blue,green'orrole_ids = '1,4,9'. You cannot join, index or constrain the items. Fix: a child table with one row per item, or a join table for many-to-many. - Entity-attribute-value (EAV). One table of
(entity_id, attribute, value)to be "flexible". Every value is text, nothing is required, and a simple report needs a pivot. Fix: real columns for known fields; a typed JSONB column for the truly open part. - Money as float. Binary floating point cannot represent 0.10 exactly, so sums drift by fractions of a cent. Fix:
numeric(12,2)or integer minor units, plus a currency code. - Missing foreign keys.
user_id integerwith noREFERENCES. Orphans appear after the first manual delete. Fix: declare it. - Timestamps without time zone.
timestampstores a wall-clock reading with no zone. Two servers in different zones write different instants as the same value, and daylight saving shifts them. Fix:timestamptz, which stores an absolute instant.
Before and after
-- Weak: AI scaffold
CREATE TABLE invoices (
id serial PRIMARY KEY,
customer_id int,
amount float,
item_ids text,
created_at timestamp
);
-- Strong
CREATE TABLE invoices (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (id),
amount numeric(12,2) NOT NULL CHECK (amount >= 0),
currency char(3) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE invoice_items (
invoice_id bigint NOT NULL REFERENCES invoices (id) ON DELETE CASCADE,
item_id bigint NOT NULL REFERENCES items (id),
PRIMARY KEY (invoice_id, item_id)
);
What goes wrong
- Flexibility as an excuse. "We might need new attributes" is how EAV tables start, and nobody ever removes them.
- Checking only that it runs. The scaffold works in a demo with three rows. The review has to read the types.
- Fixing the column but not the data. Converting float to numeric keeps the old rounding errors already stored.
- Spotting one and stopping. Generated schemas usually carry several of these at once.
Examples
The tags column
An assistant stores posts.tags = 'ai,sql,tips'. Finding every post tagged sql needs a LIKE that also matches sqlite. The fix is a post_tags (post_id, tag) table with a primary key on both columns.
The float total
A shop sums 10,000 line items stored as float and the invoice total is off by 0.03. Changing the column to numeric(12,2) makes the sum exact; the already-wrong stored totals still need a one-time correction.
Key points
- Comma lists in a column cannot be joined, indexed or constrained; use a child or join table.
- An entity-attribute-value table trades safety for flexibility and makes every report a pivot.
- Store money as numeric or integer minor units with a currency, never as float.
- A column that holds an id still needs REFERENCES, or orphans will appear.
- Use timestamptz for instants, because timestamp without a zone is ambiguous across servers and daylight saving.
Common mistakes
- Accepting a generated schema because the demo worked.
- Using float for prices, taxes or balances.
- Choosing an EAV table so that any field can be added later without a migration.
- Storing local times without a zone for events that happen across regions.
Check yourself · 2 of 3
Why is float a poor type for storing prices?
Check yourself · 3 of 3
Servers in two time zones write created_at into a timestamp without time zone column. What goes wrong?
Practice items, not counted towards your mastery.
Start here
- PostgreSQL: Data definition (opens in a new tab)DocsFree
How tables, constraints, row security and schema changes are declared in PostgreSQL.
- PostgreSQL: Constraints (opens in a new tab)DocsFree
Exact behaviour of CHECK, NOT NULL, UNIQUE, primary and foreign keys and their ON DELETE actions.
- PostgreSQL: ALTER TABLE (opens in a new tab)DocsFree
Which ALTER TABLE forms rewrite the table or take strong locks, and the NOT VALID option.
More for this topic
- SQL Antipatterns (Pragmatic Bookshelf) (opens in a new tab)BookPaid
Names and fixes for comma lists, entity-attribute-value tables and other schema traps.