How do you parse values from a join query into a struct with substructs

EDIT: so it seems PGX is not good for this. its a low level driver and means i would have to scan results. Can anyone suggest a better approach to the below problem?

So i am using pgx right now but if it cant be done with that im happy to change.

Is there a way to easily parse substruct values from a query?

So I do a Join query like:

rows, err := conn.Query(context.Background(),
    `SELECT AS r_Id,
        rdr.field1 AS r_Field2,
        rdr.field2 AS r_Field2,
        contact.firstname AS c_FirstName,
        contact.lastname AS c_LastName, AS c_Id 
    FROM rdr
    INNER JOIN contact ON`)

Then my structs are

type Rdr struct {
    Id              int32     
    Field1          int32     
    Contact         Contact   
    Field2          time.Time

type Contact struct {
    Id        int
    FirstName string
    LastName  string

How can I parse the data returned in the rows into my Rdr struct. I would need to parse the correct fields into the Contact, ideally at the same time, but i dont see how to do that. I cant see how Scan would do it. We were looking at the FieldDescriptions and the Values but it gets very messy. Is there an easy/common way to do this? I imagine its a very common issue.

If instead of Contact Contact, you change it to an embedded struct field (i.e. just Contact), then should work.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.