Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 6 min read How-to mediumAdept

The silent syntax difference in foreign keys between Postgres and MySQL

MySQL parses but silently ignores inline column-level `REFERENCES` clauses, so a foreign key written Postgres-style (`customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE`) creates no constraint, no index and no cascade behavior in MySQL. Orphaned rows insert without error. Only a separate table-level `FOREIGN KEY (...) REFERENCES ...` clause is enforced. Use the explicit `FOREIGN KEY` form in both databases, review schemas when porting between them, and test that constraints actually reject invalid data.

Assumes: SQL DDL basicsrelational foreign keys

Related posts

  • Fix a top cause of slow queries in PostgreSQL (no slow query log needed)

    Render mediumAdept

    PostgreSQL automatically indexes primary keys and UNIQUE columns but not the referencing column of a foreign key, so lookups on that side and ON DELETE CASCADE / SET NULL triggers fall back to full table scans. In a simulated blog schema with 13 million comments, deleting a single user took 19 seconds, almost all of it in foreign-key triggers. Adding non-unique B-tree indexes on articles.author_id, comments.author_id and comments.article_id cut the delete to 342 ms, a 55x speedup, with the comments-by-article trigger dropping from 18 s to 26 ms. The rule of thumb is to index foreign key columns in referencing tables, and it is mandatory when ON DELETE propagation is defined.

  • LLM Rules for Postgres

    Neon mediumAdept

    Six explicit Postgres rules, drawn from building the wispbit review bot, let an LLM code reviewer or coding agent catch common migration and query mistakes before production. The rules are to create indexes CONCURRENTLY to avoid write locks, mark columns as ignored in the ORM before dropping them, index every foreign key, require id/created_at/updated_at on every table, skip indexes already covered by a composite index, and ensure frequent WHERE conditions have matching indexes. Each comes with bad/good SQL, Prisma or SQLAlchemy examples. Teams are encouraged to add new rules after each incident.

  • Postgres to ClickHouse: Data Modeling Tips

    ClickHouse mediumAdept

    Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (_peerdb_version, _peerdb_is_deleted), so duplicates persist until background merges run. Deduplicate at query time with FINAL, argMax, ROW_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.

  • Remember to use primary keys vs unique constraints in Postgres

    Neon mediumAdept

    Postgres id columns should be declared PRIMARY KEY rather than UNIQUE, because unique constraints permit multiple NULLs and do not automatically serve as the replica identity for logical replication. Primary keys combine uniqueness with NOT NULL, create an index, act as the usual foreign-key target, and help the query planner. If you rely on UNIQUE, you need to add NOT NULL and run ALTER TABLE ... REPLICA IDENTITY USING INDEX yourself. The simpler habit is id SERIAL PRIMARY KEY, with UNIQUE kept for naturally unique columns like emails or SKUs.

  • Safely Adding NOT NULL Columns to Your Database Tables

    Shopify Engineering mediumAdept

    Adding a NOT NULL column through LHM's shadow-table migration (new table, replication triggers, batched copy, rename) can break writes or silently lose data depending on the DEFAULT clause, an added UNIQUE INDEX and the MySQL SQL mode. Shopify's experiment matrix shows that without a DEFAULT, strict mode makes trigger-propagated INSERT/UPDATE/DELETE fail, while non-strict mode fills rows with implicit defaults such as the empty string. Adding a UNIQUE INDEX in the same migration loses rows on INSERT and UPDATE whenever duplicates exist. Always specify a DEFAULT for new NOT NULL columns, and check for duplicate values before adding a unique index.

The quest for software engineering knowledge.

Look:
Layout: