'Map as a many2many association

In my current code that I want to enable to work with gorm there are many many2many associations used with map[unit]Entity instead of []Entity. Here's a simple example:

type Entity1 struct {
    gorm.Model

    Entities2 Entities2
}

type Entities2 map[uint]Entity2

type Entity2 struct {
    gorm.Model
}

How can I make gorm handle this case? In my mind gorm has to understand two things:

  1. When reading Entities2 from a db it must put the not into a slice but to a map where a key is just an Entity2's primary key.

  2. When writing Entities2 to a db it must just take a slice of map's values and save them in a db.

Is it possible at all?

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source