Hello, I’ve recently learned a bit about SQL and I’ve been looking at getting a data structure setup in Go.
I’ve been able to insert and read from the database but I’m having a hard time understanding how I should setup a one-to-many relationship using the pq library for postgres. As of right now I have two structs defined as such
type ImageSearch struct {
ID int
SearchQuery string
ImageInfo []ImageInfo
}
type ImageInfo struct {
ID int
Name string
ImageURL string
ThumbnailURL string
HostPageURL string
ImageSearchID int
}
Then I am creating the tables like this
func CreateSchema() {
imageSearch := `
CREATE TABLE IF NOT EXISTS image_search(
id SERIAL PRIMARY KEY NOT NULL,
search_query TEXT NOT NULL
);`
imageInfo := `
CREATE TABLE IF NOT EXISTS image_info(
id SERIAL PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
image_url TEXT NOT NULL,
thumbnail_url TEXT NOT NULL,
hostpage_url TEXT NOT NULL,
image_search_id INT references image_search(ID)
);`
_, err1 := DB.Exec(imageSearch)
if err1 != nil {
log.Fatal(err1)
}
_, err2 := DB.Exec(imageInfo)
if err2 != nil {
log.Fatal(err2)
}
}
What I’m basically doing is using an API that gets images, I want to save the string that I used to search along with all of the different records returned, in this case 8 of them. Each of them has various information.
If anyone could steer me in the right direction I’d really appreciate it, thank you!