Closed CmpHDL closed 11 months ago
I downgraded to 2.7.0 and it still does not seem to be working. I'm 100% positive it was working before though. So strange.
It was working before not because of a different version but because I didn't need/have the Org
before so the table.Cs.AllColumns
wasn't there. After I added it, it only appeared to have been working because one record would return fine and there's usually only 1-2 but it was truly always returning 1 record even if there were two.
Since CStruct
embeds model.Cs
(and I assume it has a field with a primary key tag), all the query rows are now grouped around model.Cs
primary key. You'll always get a single' R' result if all the rows have the same Cs
primary key.
To fix it, you can change the destination, for instance:
type R struct {
Org CStruct `json:"-"`
List []struct {
PField decimal.Decimal `json:"p" alias:"p"`
Class string `json:"class" alias:"class"`
}
}
You'll also need to update query aliases accordingly.
Or you can introduce another primary key field so the results are not grouped only over Cs
primary key.
type R struct {
SomeID int `sql:"primary_key"`
PField decimal.Decimal `json:"p" alias:"p"`
Class string `json:"class" alias:"class"`
Org CStruct `json:"-"`
}
Sweet and thank you @go-jet that makes sense, I'm trying to implement this now.
This will kinda nerf any min/max or similar grouped queries that also have any structs since you can't GROUP_BY
the MIN
's ID. Is there a way we can rows.Next()
or similiar to bypass this? Your first suggestion will probably work fine, just brainstorming.
I can see why it does this but not sure I agree that it should work this way. Is it using a map under the hood which uses the first primary key it finds?
Since
CStruct
embedsmodel.Cs
(and I assume it has a field with a primary key tag), all the query rows are now grouped aroundmodel.Cs
primary key. You'll always get a single' R' result if all the rows have the sameCs
primary key. To fix it, you can change the destination, for instance:type R struct { Org CStruct `json:"-"` List []struct { PField decimal.Decimal `json:"p" alias:"p"` Class string `json:"class" alias:"class"` } }
This worked, thank you! I just made an anonymous dest
and looped over over it to recreate the slice.
Still curious about situations where looping through again would be less ideal.
Is there a way we can rows.Next() or similiar to bypass this?
Yes, there is a Rows method.
I can see why it does this but not sure I agree that it should work this way. Is it using a map under the hood which uses the first primary key it finds?
Yes, there is a map with an entry for each composite primary key on each level - wiki.
Describe the bug When adding a
table.Records.AllColumns
as a field in the select, instead of multiple records only one is returned. I double checked thestmt.DebugSQL()
and ran the query as is and it returned multiple records in the console.Environment
Code snippet
Not working: (works but only 1 record is returned.)
Working: (returns multiple records but obviously does not fill in Org which I need filled in) Only line that was removed was
table.Cs.AllColumns
I tried a bunch of things with aliasing, direct dest, etc. Something odd is going on with adding the select. Sorry in advanced for the letter tables.