volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.66k stars 539 forks source link

Bug in `queries.MakeStructMapping` when boil tag is `boil:",bind"` #1315

Open sarthak opened 11 months ago

sarthak commented 11 months ago

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

SQLBoiler v4.10.2

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

Doesn't matter

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

type struct JoinStruct {
  A struct {
    ColA string `boil:"cola"`
    ColB string `boil:"colb"`
  } `boil:",bind"
}

queries.MakeStructMapping(reflect.TypeOf(JoinStruct{}))

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

I expect bind mapping to be a map of form {"cola": .., "colb", ...}. But in reality the map is of form {"a.cola": ..., "a.colb"}.

Thus the bug is that boil:",bind" behaves identical to boil:"<fieldname>,bind".

How to fix?

These lines are buggy -- https://github.com/volatiletech/sqlboiler/blob/644d45d0c780eab0b17a7da9e709c3cb10781768/queries/reflect.go#L395-L400

Suggested patch is to replace if len(tag) == 0 if if len(tag) == 0 && !recurse.

stephenafamo commented 10 months ago

If you have identified where the issue is, kindly send in a PR with tests included to prevent a regression.