Consider the following tables
CREATE TABLE foo (
id INT PRIMARY KEY
)
CREATE TABLE bar (
id INT PRIMARY KEY
)
CREATE TABLE foo_bar_members (
foo_id INT NOT NULL REFERENCES foo(id) ON DELETE CASCADE ON UPDATE CASCADE,
bar_id INT NOT NULL REFERENCES bar(id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (foo_id, bar_id)
)
foo_bar_members is a relations table that connects foo and bar. View foo as the parent of bar. And I have the following Go struct:
type Foo struct {
ID int `db:"id"`
BarIDs []int
}
BarIDs is a slice of bar.id that is associated with this foo by foo.id. I want to query for something like this:
SELECT * FROM foo f INNER JOIN foo_bar_members fbm ON f.id = fbm.foo_id WHERE f.id = $1
But this query is just an example. Obvious this won't scan into Foo.BarIDs. I could always do two separate queries but I'm wondering if there are better ways. I'm using sqlx.