You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
is there any way for a string to become an sqlx.Row?
Here's a minimal reproduction with a database reproduction
create table
chats (
id serialprimary key,
name varchar(255) not null,
created_at timestampnot null default now()
)
create table
participants (
chat_id integernot nullreferences chats(id),
user_id integernot nullreferences users(id),
created_at timestampnot null default now()
primary key (chat_id, user_id)
)
create table
user (
id serialprimary key,
name varchar(255) not null,
created_at timestampnot null default now()
)
-- dummy datainsert into chats (name) values ('chat1'), ('chat2'), ('chat3')
insert into users (name) values ('user1'), ('user2'), ('user3')
insert into participants (chat_id, user_id) values (1, 1), (1, 2), (2, 2), (2, 3), (3, 1), (3, 3)
Here's the go code
package main
import (
"log"
_ "github.com/jackc/pgx/v5/stdlib"// load pgx driver for PostgreSQL"github.com/jmoiron/sqlx"
)
// Define structs to map to database tablestypeChatstruct {
IDint`db:"id"`Namestring`db:"name"`CreatedAtstring`db:"created_at"`// THIS DOES NOT WORK// Participants []Participant `db:"participants"`// THIS WORKS, but it shouldn't be necessary to marshal/unmarshalParticipantsstring
}
typeParticipantstruct {
ChatIDint`db:"chat_id"`UserIDint`db:"user_id"`CreatedAtstring`db:"created_at"`
}
funcmain() {
// Connection stringlog.Printf("postgres://postgres:postgres@localhost:5432/xdw")
// Open a connection to the databasedb, err:=sqlx.Connect("pgx", "postgres://postgres:postgres@localhost:5432/xdw")
iferr!=nil {
panic(err)
}
query:=`SELECT chats.id, chats.name, chats.created_at, array_agg( ROW( participants.chat_id, participants.user_id, participants.created_at ) ) as participants FROM public.chats JOIN public.participants ON chats.id = participants.chat_id GROUP BY chats.id, chats.name, chats.created_at`varchats []Chatrows, err:=db.Queryx(query)
iferr!=nil {
panic(err)
}
forrows.Next() {
varchatChaterr=rows.StructScan(&chat)
iferr!=nil {
panic(err)
}
}
iflen(chats) ==0 {
panic("no chats found")
}
// Iterate over the results and print the datafor_, chat:=rangechats {
log.Printf("Chat ID: %d\n", chat.ID)
log.Printf("Chat Name: %s\n", chat.Name)
log.Printf("Created At: %s\n", chat.CreatedAt)
// Iterate over the participants and print each participantfor_, participant:=rangechat.Participants {
log.Printf("Participant: %+v\n", participant)
}
log.Println("---------------")
}
}
Is there any way to parse a string as a sqlx.Row or natively scan a struct from a subrecord? Am I missing something. The workaround I found was to jsonb_agg(json_build_object()) and unmarshall.
The text was updated successfully, but these errors were encountered:
is there any way for a string to become an sqlx.Row?
Here's a minimal reproduction with a database reproduction
Here's the go code
Is there any way to parse a string as a sqlx.Row or natively scan a struct from a subrecord? Am I missing something. The workaround I found was to jsonb_agg(json_build_object()) and unmarshall.
The text was updated successfully, but these errors were encountered: