I use naming strategy
schema.NamingStrategy{
NoLowerCase: true,
SingularTable: true,
}
and have a database model like this:
type Project struct {
gorm.Model
UserID uint `gorm:"not null"`
User User
}
type Bar struct {
gorm.Model
ProjectID uint `gorm:"not null"`
Project Project
Foos []*Foo `gorm:"many2many:Bar_Foos;"`
}
type Foo struct {
gorm.Model
Bars []*Bar `gorm:"many2many:Bar_Foos;"`
ProjectID uint `gorm:"not null"`
Project Project
}
There is a many-to-many relationship between Foo
and Bar
. I want to show the user all of his Foos after he has selected a Bar. That is, I want to query for exactly those Foos where Foo.Bar.Project.UserID = $userId
and Foo.Bar.ID = $barId
. $userId
and $barId
are given in from outside.
I know that with TypeORM, this would be modeled using inner joins. I have tried this with gorm in many variations but none of them have worked. My most common issues were that gorm discarded my conditions and that it generated queries without a FROM
clause.
How can this query be performed using gorm?