Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditions for Joins #6842

Open
samadguliev opened this issue Feb 21, 2024 · 7 comments
Open

Conditions for Joins #6842

samadguliev opened this issue Feb 21, 2024 · 7 comments
Assignees
Labels
type:question general questions

Comments

@samadguliev
Copy link

samadguliev commented Feb 21, 2024

Hi! How can I write conditions for Joins? I need something like this
Joins("UserProgram", "currency_id = ? AND activated IS NOT NULL AND finished IS NULL AND error IS NULL", currencyID)

But it wont work, this type of conditions works only with Preload. And I do not want to write whole table names and stuff, like
Joins("promo_usergrogram on promo_userprogram.user_program_id = ...").Where("promo_usergrogram.currency_id = ? ...")

In documentation we have
db.Joins("Company", DB.Where(&Company{Alive: true})).Find(&users)

But how can I use it for condition like
activated (time type) IS NOT NULL AND finished(time type) IS NULL AND error (string type) IS NULL

The document you expected this should be explained

https://gorm.io

@samadguliev samadguliev added the type:question general questions label Feb 21, 2024
@samadguliev
Copy link
Author

samadguliev commented Feb 22, 2024

I found a different way for condition
db.Joins("Company").Where(`"Company"."alive" = ?`, true).Find(&users)

@wxy2077
Copy link

wxy2077 commented Feb 22, 2024

maybe you can do this

db.Preload("Company", func(tx *gorm.DB) *gorm.DB {
      return tx.where("alive = ?", true)
})

@samadguliev
Copy link
Author

samadguliev commented Feb 22, 2024

@wxy2077 yeah but i need Where conditions for Joins

@zuzuviewer
Copy link

How can I write conditions for InnerJoins, above solutions aren't work

@samadguliev
Copy link
Author

@zuzuviewer I don't know about InnerJoins, but this worked for Joins
#6842 (comment)

@zuzuviewer
Copy link

@zuzuviewer I don't know about InnerJoins, but this worked for Joins #6842 (comment)

I have tried it with 1.25.5. It didn't work and not in join condition

@zuzuviewer
Copy link

zuzuviewer commented Apr 10, 2024

I found a way to resolve:
`tx := db.Table("user")

tx.InnerJoin("Company",db.Table("company").Where("id = ?",id))`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

4 participants