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

Bug: .Bind() is not working properly when selecting specific columns #1309

Open
amangupta25 opened this issue Sep 26, 2023 · 3 comments
Open
Labels

Comments

@amangupta25
Copy link

amangupta25 commented Sep 26, 2023

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v4.15.0

What is your database and version (eg. Postgresql 10)

psql (PostgreSQL) 15.4

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

Further information. What did you do, what did you expect?

I am trying to execute sql query using db_models.NewQuery(..).Bind(...) to select some specific columns (not all '*') from my psql table but it is returning me results with all fields set to the default value of their datatype and not the actual value fetched from the table. However, when i am using queries.Raw(..).Bind(..) with the same query, it is working as expected and returning correct results. Below is the struct and the code snippet that i am executing:

type LimitedInfo struct {
    ID      string `boil:"id"`
    Name    string `boil:"name"`
    EmailID string `boil:"email_id"`
}

var info []*LimitedInfo
var info1 []*LimitedInfo

err = db_models.NewQuery(qm.Select("user.id", "user.name", "user.email_id"), qm.From("user"), qm.InnerJoin("contact ON user.contact_number = contact.contact_number")).Bind(ctx, d.db, &info)
err = queries.Raw(`select user.id, user.name, user.email_id from user inner join contact ON user.contact_number = contact.contact_number`).Bind(ctx, d.db, &info1)

This results in:


info=[
  {
    ID="",
    Name="",
    EmailID="",
    
  },
  {
    ID="",
    Name="",
    EmailID="",
    
  }
]

info1=[
  {
    ID="1234",
    Name="abc",
    EmailID="abc@xyz"
  },
  {
    ID="5678",
    Name="def",
    EmailID="def@xyz"
  }
]

In the above results, 'info' has not been bound properly (its fields are not populated), but 'info1' has been bound properly.

@stephenafamo
Copy link
Collaborator

Can you turn on boil.DebugMode and share the generated queries for both?

@amangupta25
Copy link
Author

amangupta25 commented Oct 4, 2023

These are the generated queries:

  1. err = db_models.NewQuery(qm.Select("user.id", "user.name", "user.email_id"), qm.From("user"), qm.InnerJoin("contact ON user.contact_number = contact.contact_number")).Bind(ctx, d.db, &info) :

Generated Query:
SELECT "user"."id" as "user.id", "user"."name" as "user.name", "user"."email_id" as "user.email_id" FROM "user" INNER JOIN contact ON user.contact_number = contact.contact_number;

  1. err = queries.Raw(select user.id, user.name, user.email_id from user inner join contact ON user.contact_number = contact.contact_number).Bind(ctx, d.db, &info1) :

Generated Query:
select user.id, user.name, user.email_id from user inner join contact ON user.contact_number = contact.contact_number

@stephenafamo
Copy link
Collaborator

The issue is not the Bind the issue is the Join

When there is a join, the columns are aliased because it is assumed that you want to select values from multiple tables.

Try something like this:

type LimitedInfo struct {
    ID      string `boil:"id"`
    Name    string `boil:"name"`
    EmailID string `boil:"email_id"`
}

var info []*Struct{
    LimitedInfo   `boil:"user,bind"`
}


err = db_models.NewQuery(qm.Select("user.id", "user.name", "user.email_id"), qm.From("user"), qm.InnerJoin("contact ON user.contact_number = contact.contact_number")).Bind(ctx, d.db, &info)

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

No branches or pull requests

2 participants