Reputation: 51
I know I can chain several Where clauses using gorm, but can I do the same with several Select clauses ?
I've tried something like this with no luck:
query.Select("field1, field2").Select("field3").Find(&models)
The fact is that I need to chain different select clauses depending on certain conditions. How to achieve this?
Upvotes: 0
Views: 745
Reputation: 2235
Chaining multiple Select()
calls won't work, as it will only apply the last one. So in your example, the query will look like:
SELECT "field3" FROM "model";
Select()
also accepts a []string
, so instead, make a slice of strings representing the fields that you need to SELECT
, and append to it other columns under certain conditions:
selects := []string{"field1", "field2"}
if condition {
selects = append(selects, "field3")
}
query.Select(selects).Find(&models)
Upvotes: 1