dbr icon indicating copy to clipboard operation
dbr copied to clipboard

structs one-to-many relation

Open lycifer3 opened this issue 4 years ago • 2 comments

is it possible to create structures one to many db: clickhouse 20.3.5.21

example:

type DomainType struct {
	Id        string      `json:"id"`
	Domain    string      `json:"domain"`
	Date      time.Time   `json:"date"`
	Redirects []*Redirect `json:"redirects"`
}

type Redirect struct {
	Id             sql.NullString `json:"id"`
	RedirectDomain sql.NullString `json:"redirect_domain" db:"redirect_domain"`
	Date           sql.NullTime   `json:"redirect_date" db:"date"`
}

end query:

var result []entity.DomainType
query := sess.Select("*").From("domain").LeftJoin("domain_redirect", "domain.id = domain_redirect.id")

query.LoadStructs(&result)

if this is possible then tell me how, I'm begginer to the golang

lycifer3 avatar Apr 15 '20 11:04 lycifer3

Hi! When you join like that, You will be having rows for a struct like this

type DomainType struct {
	Id        string      `json:"id"`
	Domain    string      `json:"domain"`
	Date      time.Time   `json:"date"`
	RedirectDomain sql.NullString `json:"redirect_domain" db:"redirect_domain"`
	Date           sql.NullTime  `json:"redirect_date" db:"date"`
}

DoubleDi avatar Apr 15 '20 16:04 DoubleDi

I understand that, the question is to get a one-to-many structure

lycifer3 avatar Apr 15 '20 18:04 lycifer3