Skip to content

GORM Migration: Column Change Not Applied

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about GORM Migration: Column Change Not Applied. We cover key concepts, practical examples, and best practices.

Schema migration with GORM -- Use GORM's Migrator interface for fine-grained schema changes beyond AutoMigrate.

The Problem

AutoMigrate only adds new tables, columns, indexes. It does NOT modify existing column types. Use db.Migrator() methods.

Wrong

db.AutoMigrate(&User{})
// User has Name string, want TEXT type

Output:

// Column remains VARCHAR, not changed
db.Migrator().AlterColumn(&User{}, "Name")
db.Migrator().RenameColumn(&User{}, "old_name", "name")
db.Migrator().DropColumn(&User{}, "Email")

Output:

// Column type changed to TEXT

Prevention

  • Use db.Migrator() for column type changes
  • AutoMigrate is additive only
  • Check with HasColumn before dropping
  • Use CreateTable, DropTable for table operations
  • Write versioned migrations for production

Common Mistakes with gorm migration

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**Does Migrator work across all databases?**

Most methods are portable. Some DB-specific limits apply.

How do I add an index?

Use db.Migrator().CreateIndex(&User{}, "Name") or gorm:"index" tag.

Can I run raw DDL?

Yes. db.Exec("ALTER TABLE users ADD COLUMN age INT").


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro