---
title: "Safely Adding NOT NULL Columns to Your Database Tables"
description: "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."
url: "https://swe.quest/articles/safely-adding-not-null-columns-to-your-database-tables-0c4b04a1141f0507"
canonical: "https://shopify.engineering/add-not-null-colums-to-database"
source: "Shopify Engineering"
published: "2022-10-04"
difficulty: "medium"
kind: "Research"
reading_minutes: 7
tags: ["schema-migrations","mysql","databases"]
---

# Safely Adding NOT NULL Columns to Your Database Tables

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-10-04, 7 min read, Research. Original: [https://shopify.engineering/add-not-null-colums-to-database](https://shopify.engineering/add-not-null-colums-to-database)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [MySQL](https://swe.quest/tags/mysql.md) (Data & databases)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- MySQL triggers ([easy posts on MySQL](https://swe.quest/tags/mysql.md?difficulty=easy))
- online schema change tools
- SQL constraints and indexes ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

Large Hadron Migrator, LHM, MySQL, STRICT\_ALL\_TABLES, shadow table

## Related posts

- [Navigating PostgreSQL Locks with Neon and Atlas](https://swe.quest/articles/navigating-postgresql-locks-with-neon-and-atlas-61f1baeebb69a86a.md): Neon · 2025-03-14 · Medium · 11 min read · Schema migrations, PostgreSQL, CI/CD, Static analysis
  Adding a UNIQUE constraint or setting a column NOT NULL in Postgres triggers a full table scan under an ACCESS EXCLUSIVE lock, which blocks all reads and writes and can take down an API on large tables.
- [LLM Rules for Postgres](https://swe.quest/articles/llm-rules-for-postgres-ad82e8ad652f6e5a.md): Neon · 2025-08-29 · Medium · 4 min read · PostgreSQL, Code review, Schema migrations, AI for coding, LLMs
  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 silent syntax difference in foreign keys between Postgres and MySQL](https://swe.quest/articles/the-silent-syntax-difference-in-foreign-keys-between-postgres-and-mysql-36df1bac8b9567ad.md): Neon · 2024-06-05 · Medium · 6 min read · MySQL, PostgreSQL, SQL, Schema migrations
  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.
- [The State of Online Schema Migrations in MySQL — PlanetScale](https://swe.quest/articles/the-state-of-online-schema-migrations-in-mysql-planetscale-1e08889c563423ee.md): PlanetScale · 2024-07-23 · Medium · 9 min read · Schema migrations, MySQL
  MySQL's native INPLACE online DDL is unsuitable for non-blocking changes because it is resource-greedy, uninterruptible and blocks replicas, causing replication lag as long as the original ALTER.
- [Fix a top cause of slow queries in PostgreSQL (no slow query log needed)](https://swe.quest/articles/fix-a-top-cause-of-slow-queries-in-postgresql-no-slow-query-log-needed-4d1a4c0df1aa9a0d.md): Render · 2024-12-12 · Medium · 7 min read · PostgreSQL, Performance engineering, SQL
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
