Your Question
I have a database which I do not control and cannot alter. Pardon the pseudo-code, only to illustrate the idea.
type Prod struct {
ID int `gorm:"column:PK_PROD;primary_key;auto_increment:false"`
Name string
Groups []Group `gorm:"many2many:PROGR;foreignKey:PK_PROD;joinForeignKey:PK_PROD;References:PK_GROUP;joinReferences:PK_GROUP"`
}
// many2many table is named PROGR
// int PK_PROD
// int PK_GROUP
type Group struct {
ID int `gorm:"column:PK_GROUP;primary_key;auto_increment:false"`
Name string
}
db.Preload('Groups').Find(&products);
The problem is, that this produces altered SQL, the m2m table name is pluralized (letter s added) and lowercased resulting in an error - SELECT * FROM "progrs" WHERE "progrs"."pk_prod" IN (1,2,3) mssql: Invalid object name 'progrs'
Also the key names (PK_PROD) are lowercased.
The document you expected this should be explained
https://gorm.io/docs/many_to_many.html#Many-To-Many
Expected answer
If I specify many2many relation, I expect the name of the table not to be altered in any way.
Like with other relationships e.g. gorm:"foreignKey:PK_PROD;references:PK_PROD" keeps correct uppercase.
How to disable this behavior? I need relationship names unchanged - uppercase, singular.
Comment From: jinzhu
https://gorm.io/docs/gorm_config.html#NamingStrategy
Maybe use the NoLowerCase option of NamingStrategy? or you can totally customize the naming logic.