From c03efd4a6510802184189e056e1bc0eb5ab4ce29 Mon Sep 17 00:00:00 2001 From: Tim McCarthy Date: Mon, 15 Jun 2026 10:38:41 -0700 Subject: [PATCH] Fix SQL migration error --- AGENTS.md | 3 +++ .../5a6e73d2bc4f_add_success_failure_stakes_to_challenge.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0184312..48b1d50 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,6 +11,9 @@ After editing `src/pirats/models.py`, generate a migration and sanity-check it: .venv/bin/alembic upgrade head # or just start the app ``` +**Note on adding NOT NULL columns in SQLite:** +SQLite does not support adding a `NOT NULL` column without a default value to an existing table. When Alembic auto-generates a migration that adds a `nullable=False` column, you MUST manually edit the migration to include `server_default="..."` in the `sa.Column` definition (e.g., `server_default=""` for strings) before applying it. Otherwise, the migration will crash with `Cannot add a NOT NULL column with default value NULL`. + Do NOT add ad-hoc `ALTER TABLE` statements to `database.py` — that legacy list exists only to upgrade pre-Alembic databases to the baseline and must not grow. ## Learning during testing diff --git a/src/pirats/migrations/versions/5a6e73d2bc4f_add_success_failure_stakes_to_challenge.py b/src/pirats/migrations/versions/5a6e73d2bc4f_add_success_failure_stakes_to_challenge.py index 5d8b937..e8346c5 100644 --- a/src/pirats/migrations/versions/5a6e73d2bc4f_add_success_failure_stakes_to_challenge.py +++ b/src/pirats/migrations/versions/5a6e73d2bc4f_add_success_failure_stakes_to_challenge.py @@ -19,8 +19,8 @@ depends_on = None def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('challenge', schema=None) as batch_op: - batch_op.add_column(sa.Column('stakes_success', sqlmodel.sql.sqltypes.AutoString(), nullable=False)) - batch_op.add_column(sa.Column('stakes_failure', sqlmodel.sql.sqltypes.AutoString(), nullable=False)) + batch_op.add_column(sa.Column('stakes_success', sqlmodel.sql.sqltypes.AutoString(), server_default="", nullable=False)) + batch_op.add_column(sa.Column('stakes_failure', sqlmodel.sql.sqltypes.AutoString(), server_default="", nullable=False)) # ### end Alembic commands ###