Skip to content

Failing to parse size constraint silently ignores it #7595

@markusthoemmes

Description

@markusthoemmes

Description

When using a size constraint in the struct tag, that can't be parsed as a number correctly, the size constraint is silently ignored and thus later not applied to the database. I've run into this because of a typo in the struct tag, where I did something like this

type Foo struct {
    Bar string `gorm:"size:32,index`
}

Notice the , vs ;.

I would've expected the migration to fail on this because the settings aren't correctly parseable but sadly, it didn't.


I spelunked the code a little and I think

gorm/schema/field.go

Lines 215 to 219 in b881483

if num, ok := field.TagSettings["SIZE"]; ok {
if field.Size, err = strconv.Atoi(num); err != nil {
field.Size = -1
}
}
is where it's supposed to be dealt with and wonder if we "just" need to set schema.err in that case (and in some other cases too, maybe?). I don't know the code base well enough to judge that though.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions