Gorm: Multiple joins

Created on 3 Jan 2016  路  4Comments  路  Source: go-gorm/gorm

Is it possible to chain Joins? As I try Joins().Joins() it generates query with last join. What should I do if I need 2 joins and for each of them I need where condition?

Most helpful comment

Also, a heads up, the documentation is misleading about this. Under Query at the end in the Joins section it mentions:

// multiple joins with parameter
db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "[email protected]").Joins("JOIN credit_cards ON credit_cards.user_id = users.id").Where("credit_cards.number = ?", "411111111111").Find(&user)

All 4 comments

You could only write one big Joins conditions for now, like:

db.Joins("left joins xxx on 2=2 left joins yyy on 1=1").Find(&users)

This would be very useful to build up a query with different joins depending on program logic

This would be a useful to have this!

Also, a heads up, the documentation is misleading about this. Under Query at the end in the Joins section it mentions:

// multiple joins with parameter
db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "[email protected]").Joins("JOIN credit_cards ON credit_cards.user_id = users.id").Where("credit_cards.number = ?", "411111111111").Find(&user)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kumarsiva07 picture kumarsiva07  路  3Comments

Quentin-M picture Quentin-M  路  3Comments

koalacxr picture koalacxr  路  3Comments

zeropool picture zeropool  路  3Comments

satb picture satb  路  3Comments