protoc-gen-gorm
protoc-gen-gorm copied to clipboard
Incorrect Order of columns
It looks like the order of columns becomes alphabetical, regardless of how the column names are ordered in the proto file. This is a major issue when migrating to the new schema. Is there a way to preserve the order of columns as specified in the proto file?
https://github.com/infobloxopen/protoc-gen-gorm/blob/fe2de4ff3993016a6cec674884c453d1ef7f0063/plugin/plugin.go#L422
Hmmm. Currently there isn't a built-in way. One issue with that would be that it wouldn't know where to insert any of the model-only include
fields.
I wasn't aware of use cases where the ordering of the columns was especially important (aside from DB optimizations). Is the problem related to the gorm.AutoMigrate
functionality?
That's right, gorm.AutoMigrate does now work as intended. Generated tables have columns ordered alphabetically, which is problem if schema/gorm object is to change on next deployment. To resolve this, I simply used golang-migrate/migrate for what was previously migrated by gorm.AutoMigrate. This is arguably a better solution. However, gorm uses reflect.Field(i) to get struct fields, order of struct fields matters for AutoMigrate. I would expect generated gorm struct fields to be ordered the same as defined in proto.