Skip to content

Commit 5217937

Browse files
getevoclaude
andcommitted
fix(mysql/ddl): DEFAULT NULL must imply nullable column
When a field carries `default:null` (e.g. SoftDeletedAt) GORM sets DefaultValue="null" but the column.Nullable flag was never set to true unless the Go type was a pointer or the NULLABLE tag was present. This produced the contradictory DDL: `deleted_at` timestamp DEFAULT null NOT NULL which MySQL/MariaDB rejects with Error 1067. Fix: after the pointer/NULLABLE check, force column.Nullable=true whenever column.Default is "null" (case-insensitive). A column that defaults to NULL cannot be NOT NULL. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b2ec413 commit 5217937

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/mysql/ddl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ func fromStatementToTable(stmt *gorm.Statement) ddlTable {
142142
}
143143
}
144144

145+
// A column whose default value is NULL must be nullable — regardless of
146+
// whether the Go type is a pointer. Emitting DEFAULT NULL NOT NULL is
147+
// contradictory and causes MySQL/MariaDB error 1067.
148+
if strings.EqualFold(column.Default, "null") {
149+
column.Nullable = true
150+
}
151+
145152
if (column.Type == "TIMESTAMP" || column.Type == "timestamp") && column.Default == "" && !column.Nullable {
146153
column.Default = "CURRENT_TIMESTAMP"
147154
}

0 commit comments

Comments
 (0)