GORM Playground Link
https://github.com/go-gorm/playground/pull/555
Description
When a new boolean colum is added, AutoMigrate does not set it to the default value for existing records (at least for sqlite). This might also affect other data types and databases.
Expected result
I expect AutoMigrate to migrate empty columns to the default value.
This has two reasons:
- Queries for that column will use
users.active = 1orusers.active = 0, where this record will not match none (see reproduction) - A boolean in go can only be
trueorfalse. There is no way to use a struct query to find those fields, I would need to use an explicit query forIS NULLto get any of these rows back
Comment From: a631807682
To protect the data, AutoMigrate will not change the existing records.
Comment From: morremeyer
So we need to write around that in every case?
I understand this behavior for changes to existing columns, but for a new column, having to differentiate between "no value set" and "value set" is a lot of code every time.
I would expect the ORM to handle this or provide me with the option to migrate the data.
Currently, the migration leaves the database in what I would call an inconsistent state since there is data that does not conform to the specification I configured.
Comment From: a631807682
So we need to write around that in every case?
I understand this behavior for changes to existing columns, but for a new column, having to differentiate between "no value set" and "value set" is a lot of code every time.
I would expect the ORM to handle this or provide me with the option to migrate the data.
Currently, the migration leaves the database in what I would call an inconsistent state since there is data that does not conform to the specification I configured.
In fact, you can query or insert using sql.NullBool, the problem here is that we do not think that the basic type is NOT NULL, but we cannot set the basic type to NULL.
You can add not null tag for it.
Active bool `gorm:"not null"`