Hello! Hope everything's fine with you.

Just doing some experimentation with GORM and I found myself having a hard time trying to achieve the following result. I have these 3 models:

type Profile struct {
    Id                  *uuid.UUID          `gorm:"type:uuid;primaryKey"`
    Slug                    string              `gorm:"not null;uniqueIndex"`
    Name                    string              `gorm:"not null"`
    Description                     sql.NullString
    CreatedAt               time.Time           `gorm:"not null"`
    UpdatedAt               time.Time           `gorm:"not null"`
    DeletedAt               sql.NullTime

    Users                   []*User             `gorm:"many2many:users_profiles_access;"`
}

type User struct {
    Id          *uuid.UUID              `gorm:"type:uuid;primaryKey"`
    Name            string                  `gorm:"not null"`
    CreatedAt       time.Time               `gorm:"not null"`
    UpdatedAt       time.Time               `gorm:"not null"`
    DeletedAt       sql.NullTime

    Profiles        []*Profile               `gorm:"many2many:users_profiles_access;"`
}

type UserProfileAccess struct {
    UserId      *uuid.UUID      `gorm:"type:uuid;primaryKey;"`
    ProfileId   *uuid.UUID      `gorm:"type:uuid;primaryKey;"`
    AccessLevel int8             `gorm:"not null"`
    CreatedAt   time.Time       `gorm:"not null"`
    UpdatedAt   time.Time       `gorm:"not null"`
    DeletedAt   sql.NullTime

    User        *User
    Profile      *Profile
}

func (UserProfileAccess) TableName () string {
    return "users_profiles_access"
}

And on main.go, I call this:

func migration(db *gorm.DB) {
    errProfileUserJoin := db.SetupJoinTable(&entities.Profile{}, "Users", &entities.UserProfileAccess{})

    if errProfileUserJoin != nil {
        panic(errProfileUserJoin)
    }

    errUserProfileJoin := db.SetupJoinTable(&entities.User{}, "Profiles", &entities.UserProfileAccess{})

    if errUserProfileJoin != nil {
        panic(errUserProfileJoin)
    }

    db.AutoMigrate(
        &entities.User{},
        &entities.Profile{}
    )
}

Things are working well and the tables are correctly generated, but the question is: how do I access an array of UserProfileAccess from Profile or User, so I can get (UserId, ProfileId)'s AccessLevel? And how to get to Profile or User from a UserProfileAccess?

Couldn't find an explanation on how to access the custom JoinTable here...

Cheers.

Comment From: jinzhu

https://github.com/go-gorm/gorm.io/commit/654fd7e8b5e2f431b4f104653f483dd83b841c41

so I can get (UserId, ProfileId)'s AccessLevel? And how to get to Profile or User from a UserProfileAccess

You could use Preload, Join Preload, Association Mode everything that you can use for normal structs

Comment From: zhanghonged

Can you give me an example of how to use it?

Comment From: BDu4rTe

any update?