Skip to content

Many-to-Many and attributes #719

@Quentin-M

Description

@Quentin-M

Hi !

I was wondering what would be the best way to create a many-to-many relationship with extra attributes ? For instance, I'd like to have Library <--> LibraryHasBook (count) <--> Books. LibraryHasBook would have the following fields (library_id, book_id, count). Has anyone done or seen that pattern before ?

I was thinking of:

type Library struct {
    ID            uint   `gorm:"primary_key"`
    Name      string
    Books     []LibraryHasBook
}

type LibraryHasBook struct {
    Library   Library   `gorm:"primary_key"`
    Book Book `gorm:"primary_key"`
    Count   uint
}

type Book struct {
    ID            uint   `gorm:"primary_key"`
    Name      string
    Libraries  []LibraryHasBook
}

And then inserting a new Book & associated Libraries with something like:

book := Book{
    Name: "The Lords of the Ring",
    Libraries: []LibraryHasBook {
        LibraryHasBook{
            Library: Library{Name: "Tolkien Specialists"},
            Count: 10,
        },
        LibraryHasBook{
            Library: Library{Name: "Awesome Library"},
            Count: 3,
        },
    },
}
db.Create(&book)

But I am quite not sure if it would work (the Go is most likely not valid anyways as I typed it directly in that issue), especially if GORM would be able to fill LibraryHasBook's Library, and what annotation should be used, etc. Should I instead use a transaction, create Libraries, create Book, then LibraryHasBooks, specifying explictely the book/librairies and commit, would it work better ?

How would it be possible then to get a book with its Libraries and count ?

I did not find anything related in the docs, pardon me if I am blind.
Thank you so much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions