protoc-gen-gorm icon indicating copy to clipboard operation
protoc-gen-gorm copied to clipboard

Incorrect Order of columns

Open jackskj opened this issue 5 years ago • 2 comments

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

jackskj avatar Aug 27 '19 16:08 jackskj

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?

Calebjh avatar Aug 27 '19 22:08 Calebjh

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.

jackskj avatar Aug 28 '19 15:08 jackskj