Currently, we define type struct model. I have a use case and I would like know what is the field name used in database.
type User struct {
CreatedAt time.Time
}
Final field name will be created_at but I can't found no method in Gorm to found this.
created_at is automatic CamelCase->snake_case conversion that gorm performs. gorm.Model uses CreatedAt as one of it's fields.User — you will not have an issue.Also, look at how you can use annotations to set the column name yourself:
CreatedAt time.Time `sql:"column:creation_date;"`
I would like determine (with a internal Gorm method), the field name generated.
CreatedAt time.Time `sql:"column:creation_date;"`
UpdatedAt time.Time
Field names given will be creation_date and updated_at.
Your internal method to determine final field name to use in database is it public ?
I don't know if you found the answer or not. but this what I found.
You can use Scope For example:
userScope := db.NewScope(User{})
createdAtField, ok := userScope.FieldByName("CreatedAt")
fieldName := createdAtField.DBName
This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you
Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog