go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.88k stars 3.93k forks source link

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

Open apuckey opened 4 months ago

apuckey commented 4 months ago

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

JunHyungJang commented 3 months ago

Is this issue still open or need to review further?

apuckey commented 3 months ago

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

injae-kim commented 3 months ago

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 :)

apuckey commented 3 months ago

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.