go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly

Home Page:https://gorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many2Many with custom join table gets converted to snake_case when with TableName() function

apuckey opened this issue · comments

Your Question

Unable to set custom many2many table, always gets converted to snake case

The document you expected this should be explained

Currently im migratin a java application to golang and the existing join tables are specifically named with Pascal Case ie: ModelOne and ModelTwo with a join table of ModelOneJoinTwo

I have a field in ModelOne as follows:

Many2ManyFieldInModelOne []*ModelTwo `json:"modelTwo"     gorm:"many2many:ModelOneJoinToTwo;foreignkey:id;association_foreignkey:id;association_jointable_foreignkey:model_two_id;jointable_foreignkey:modal_one_id"`

however it always changes the name to snakecase: model_one_join_to_two

I have tried a custom NamingStrategy but it yields the same result

I have tried with a custom join table

type ModelOneJoinToTwo struct {
	ModelOneId string `json:"modal_one_id"     gorm:"column:modal_one_id"`
	ModalTwoId  string `json:"model_two_id"     gorm:"column:model_two_id"`
}

func (m *ModelOneJoinToTwo) TableName() string {
	return "ModelOneJoinToTwo"
}

and used like so:

_ = EngineNew.SetupJoinTable(&ModelOne{}, "Many2ManyFieldInModelOne", &ModelOneJoinToTwo{})

when i debug it gets into TableName and returns the correct string but later still gets changes to snake case.

Expected answer

TableName() on the join table to translate to the database calls

Is this issue still open or need to review further?

Yes this is still happening, Unless someone has committed something recently to rectify it and not linked this issue.

someone has committed something recently to rectify it and not linked this issue.

Hi @apuckey , can you share commit link? we want to try to fix this issue :)

Hi,

I think you mis-read what i wrote. I was saying unless someone has already committed a fix then its still broken. I'll do some debugging later to try narrow down where the issue is.