[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] many to many relationship in Go Admin #634

Open
yoavweber opened this issue Aug 7, 2024 · 0 comments
Open

[Question] many to many relationship in Go Admin #634

yoavweber opened this issue Aug 7, 2024 · 0 comments

Comments

@yoavweber
Copy link

Description [describe your questions]

I'm trying to set up a many-to-many relationship in Go Admin, specifically between a Guide model and a Region model:

type Region struct {
	gorm.Model
	Name string `gorm:"type:varchar(100)"`
}
type Guide struct {
	gorm.Model
	Name    string   `gorm:"type:varchar(100)"`
	Regions []Region `gorm:"many2many:guide_regions;"`
}

'm having trouble figuring out how to properly handle this relationship in the table setup, particularly for create, update, and delete operations.
My main questions are:

How do I properly save the selected relations when creating or updating a Guide?
How do I ensure that the related records in the junction table (guide_regions) are deleted when a Guide is deleted?

Example code [If you have any code info]

func GetGuidesTable(ctx *context.Context) table.Table {
guides := table.NewDefaultTable(ctx, table.DefaultConfigWithDriver("postgresql"))

info := guides.GetInfo().HideFilterArea()
info.SetTable("guides").SetTitle("Guides").SetDescription("Guides")

info.AddField("ID", "id", db.Int).FieldSortable()
info.AddField("Name", "name", db.Varchar).FieldSortable()
info.AddField("Regions", "regions", db.Varchar).FieldDisplay(func(model types.FieldModel) interface{} {
    return model.Value
})

formList := guides.GetForm()
formList.SetTable("guides").SetTitle("Guides").SetDescription("Guides")

formList.AddField("Name", "name", db.Varchar, form.Text).FieldMust()

formList.AddField("Regions", "regions", db.Varchar, form.Select).
    FieldOptions(getRegionOptions(ctx)).
    FieldDisplay(func(model types.FieldModel) interface{} {
        return model.Value
    }).
    FieldMust()

return guides

}

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant