Your Question
Should gorm be able to change a field in the DB from NOT NULL to NULL? Based on the Auto-Migration docs, it seems like it should. However from inspection of the source, alterColumn is only set to true if the field is not primary key and column in the database is currently nullable https://github.com/go-gorm/gorm/blob/33601dc72f4abf86ce68cbb663f7f5c898bee0a3/migrator/migrator.go#L398-L404
The document you expected this should be explained
https://gorm.io/docs/migration.html#Auto-Migration
Expected answer
I would actually expect NULL to NOT NULL to be the case gorm cannot automatically handle (due to the need to update existing rows which have NULL values), but I assumed NOT NULL to NULL would be ok
Comment From: ViddeM
I've been spending hours trying to figure this one out, I read the note on the documentation page that you linked such that it should handle both ways (see below). However, in my testing it appears to not handle either way, I've made an update where I changed one field from Nullable to Not Null as well as a field that is Not Null but that I'm trying to make a Not Null field but neither changes. If I clear the database and re-run the automigrate it creates the tables correctly but it won't migrate existing tables.
NOTE: AutoMigrate will create tables, missing foreign keys, constraints, columns and indexes.
It will change existing column’s type if its size, precision, nullable changed.
It WON’T delete unused columns to protect your data.
I enabled full debugging in postgres to see what was actually communicated to the database and saw that it is actually running ALTER TABLE commands specifically for the changed columns, however, it is only setting the TYPE, not the nullability of the fields. I might very well have missed something here but this seems wrong.
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days
Comment From: steffann
I just ran into the same problem. The issue still seems to exist.
Comment From: akshaybharambe14
I am also facing the same problem.