go-gorm / sqlite

GORM sqlite driver
MIT License
179 stars 184 forks source link

Incorrect ColumnType Nullable default #158

Open ChrisPortman opened 1 year ago

ChrisPortman commented 1 year ago

Description

Sqlite coumns that are neither NOT NULL or NULL are nullable by default. However, .Nullable() defaults to false and is only set true if the CREATE TABLE sql explicitly denotes the column as NULL

https://github.com/go-gorm/sqlite/blob/397ec6fa8c64060db1dd392675cf51ab919c4c3c/ddlmod.go#L122

columnType := migrator.ColumnType{
    <snip>
    NullableValue:     sql.NullBool{Valid: true},    //<--- .Bool implicitly false, i.e. NOT NULL 
    <snip>
}

matchUpper := strings.ToUpper(matches[3])
if strings.Contains(matchUpper, " NOT NULL") {
    columnType.NullableValue = sql.NullBool{Bool: false, Valid: true}
} else if strings.Contains(matchUpper, " NULL") {
    columnType.NullableValue = sql.NullBool{Bool: true, Valid: true}
}
...
ChrisPortman commented 1 year ago

I think this is also raised by #107