Coder
Coder

Reputation: 1899

Sqlboiler get only the desired columns

I am trying to follow the examples in sqlboiler (https://github.com/volatiletech/sqlboiler). But couldn't find a way to get just the columns queried in the select statement?

users, err := models.Users(
  Select("id", "name"),
  Where("age > ?", 30),
).All(ctx, db)

In this example, .All returns entire tuple containing empty/nil values of columns not queried. I was wondering if there is a way to return a map/list (or any relevant data structure/format) of just the queried columns. Thanks!

Upvotes: 1

Views: 2667

Answers (1)

Dirbaio
Dirbaio

Reputation: 3142

You get all the fields, because you get instances of models.User, which have all the fields, you want them or not.

One thing you can do is write your own cut-down User struct, and bind to that.

type LiteUser struct {
  ID int `boil:"id"`
  Name string `boil:"name"`
}

var users []*LiteUser
err := models.Users(
  Select("id", "name"),
  Where("age > ?", 30),
).Bind(ctx, db, &users)

Upvotes: 2

Related Questions