Skip to content

fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string#234

Open
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-nil-schema-panic
Open

fix(Migrator): don't panic in AlterColumn/DropColumn when value is a table-name string#234
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-nil-schema-panic

Conversation

@h2zi

@h2zi h2zi commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What this PR does

The base GORM migrator accepts a table-name string as the value argument (RunWithValue sets stmt.Table and leaves stmt.Schema nil), and the other official dialects support this calling style. The sqlite driver dereferences stmt.Schema unconditionally in AlterColumn and DropColumn, so both crash with a nil pointer dereference:

db.Migrator().DropColumn("my_table", "my_column") // panic: invalid memory address

The fix

  • DropColumn works fine without a model schema (the given column name is used as-is), so it now just skips the field lookup, mirroring the stmt.Schema != nil guard that HasColumn already has.
  • AlterColumn cannot build the new column type without the model schema, so it returns a regular error instead of panicking.

Tests

TestMigratorStringTableName covers both paths: DropColumn with a table-name string succeeds and actually drops the column, AlterColumn returns an error instead of panicking.

🤖 Generated with Claude Code

…table-name string

The base GORM migrator accepts a table-name string as the value for
migrator methods (RunWithValue sets stmt.Table and leaves stmt.Schema
nil), and the other dialects support this. The sqlite driver
dereferenced stmt.Schema unconditionally in AlterColumn and DropColumn,
so both panicked with a nil pointer dereference:

    db.Migrator().DropColumn("my_table", "my_column")

DropColumn works fine without a schema (the column name is used as-is),
so it now just skips the field lookup. AlterColumn cannot build the new
column type without the model schema and returns an error instead of
panicking.
Copilot AI review requested due to automatic review settings July 7, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a nil-pointer panic in the SQLite GORM migrator when AlterColumn/DropColumn are called with a table-name string (i.e., stmt.Schema == nil), aligning behavior with GORM’s base migrator and other dialects.

Changes:

  • Guard DropColumn’s schema field lookup behind stmt.Schema != nil to prevent panics when called with a table-name string.
  • Make AlterColumn return a regular error (instead of panicking) when stmt.Schema == nil, since it requires schema to compute the new column type.
  • Add a regression test covering both DropColumn (success) and AlterColumn (error) for the table-name string calling style.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
migrator.go Prevents nil dereference in DropColumn and converts AlterColumn’s schema-nil panic into a regular error.
migrator_test.go Adds regression coverage for table-name-string calls to DropColumn/AlterColumn.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Keeps repeated runs (go test -count=N) starting from a clean state,
matching the review feedback on the sibling PRs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants