How can i save&get collection/slice of struct that has one to many relationship with two other structs respectively, using gorm library?

Suppose I have these three structs:

type A struct {
    Id int
    Field string
    Bs  []B
}

type B struct {
    Id int
    Field string
    Cs  []C
}

type C struct {
    Id int
    Field string
}

And I have these three tables in the test database:

create table a (
    Id int not null auto_increment,
    Field  varchar(40) not null,
    primary key (Id)
);

create table b (
    Id int not null auto_increment,
    Field varchar(40) not null,
    Fk int not null,
    foreign key (FK) references a (Id),
    primary key (Id)
);

create table c (
    Id int not null auto_increment,
    Field varchar(40) not null,
    Fk int not null,
    foreign key (FK) references b (Id),
    primary key (Id)
);

How can I save & get a collection/slice of A in my test database using gorm library?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.