left join with null #619
-
|
Is there a way to map db NULL result after left join to a nil ptr or null.Val[T]? type OrgRow struct {
models.Organization
OrganizationWorkHour *models.OrganizationWorkHour
}
// models.OrganizationWorkHour is an object representing the database table.
type OrganizationWorkHour struct {
ID int64 `db:"id,pk" `
OrganizationID null.Val[int64] `db:"organization_id" `
Monday null.Val[string] `db:"monday" `
Tuesday null.Val[string] `db:"tuesday" `
Wednesday null.Val[string] `db:"wednesday" `
Thursday null.Val[string] `db:"thursday" `
Friday null.Val[string] `db:"friday" `
Saturday null.Val[string] `db:"saturday" `
Sunday null.Val[string] `db:"sunday" `
CreatedAt time.Time `db:"created_at" `
UpdatedAt time.Time `db:"updated_at" `
R organizationWorkHourR `db:"-" `
}
func Test(ctx context.Context, exec bob.Executor, id int64) {
q := psql.Select(
sm.Columns(
models.Organizations.Columns,
models.OrganizationWorkHours.Columns.WithPrefix("organization_work_hour."),
),
sm.From(models.Organizations.Name()),
sm.LeftJoin(models.OrganizationWorkHours.Name()).On(models.Organizations.Columns.ID.EQ(models.OrganizationWorkHours.Columns.OrganizationID)),
sm.Where(models.Organizations.Columns.ID.EQ(psql.Arg(id))),
sm.GroupBy(psql.Raw("organization.id, organization_work_hour.id")),
)
query, args, err := q.Build(ctx)
if err != nil {
panic("q.Build: " + err.Error())
}
// org, err := bob.One(ctx, exec, q, scan.StructMapper[OrgRow]()) // this fails with err below
// can't scan into dest[11] (col: organization_work_hour.id): cannot scan NULL into *int64
result, err := bob.One(ctx, exec, q, scan.StructMapper[OrgRow](scan.WithTypeConverter(orm.NullTypeConverter{})))
// this works, but `result.OrganizationWorkHour` is not nil, but empty struct even though i used pointer or null.Val wraper
if err != nil {
panic("bob.One: " + err.Error())
}
godump.Dump(result.OrganizationWorkHour)
fmt.Println(query)
fmt.Println(args)
} |
Beta Was this translation helpful? Give feedback.
Answered by
stephenafamo
Jan 29, 2026
Replies: 1 comment
-
|
You'll have to pair the type converter with the unexported row validator |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
stephenafamo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll have to pair the type converter with the unexported row validator