Pg: Select() returns missing relation as zero struct instead of nil

Created on 27 Sep 2020  路  1Comment  路  Source: go-pg/pg

Select() scans the query results differently compared to Model().
With Model() missing pointer relations are represented as nil (as expected), but with Select() they are represented with their default zero value struct.

Example:

type Team struct {
    Id   int
    Name string
}

type Account struct {
    Id     int
    TeamId int

    Team *Team `pg:"rel:has-one"`
}

// ---
// teams table is empty, so there aren't any active account-team relations.
// ---

account1 := &Account{}
db.Model(account1).Relation("Team").Limit(1).Select()
// Result: account1.Team is nil (as expected)

account2 := &Account{}
db.Model((*Account)(nil)).Relation("Team").Limit(1).Select(account2)
// Result: account2.Team is a zero Team{} struct
bug

Most helpful comment

Thanks for the report - should be fixed in v10.5.1

>All comments

Thanks for the report - should be fixed in v10.5.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ValorVl picture ValorVl  路  3Comments

pahanini picture pahanini  路  4Comments

RepentantGopher picture RepentantGopher  路  5Comments

dizzyfool picture dizzyfool  路  3Comments

noggan picture noggan  路  5Comments