Associations in GOLANG using GORM

0

Good morning,

I have the following structures:

/*
* User
 */
type User struct {
    ID              uint       'gorm:"primary_key" json:"id'
    Name            string     'json:"name"'
    Email           string     'json:"email"'
    Password        string     'json:"password"'
    JobpositionID   uint       'json:"jobposition_id"'
    CreatedAt       *time.Time 'json:"created_at"'
    UpdatedAt       *time.Time 'json:"updated_at"'
    DeletedAt       *time.Time 'json:"deleted_at"'
}

/*
* Job position
 */
type Jobposition struct {
    ID          uint       'gorm:"primary_key" json:"id'
    Name        string     'gorm:"unique" json:"name"'
    Description string     'json:"description"'
    CreatedAt   *time.Time 'json:"created_at"'
    UpdatedAt   *time.Time 'json:"updated_at"'
    DeletedAt   *time.Time 'json:"deleted_at"'
}

But I do not have any ideas on how to make associations in Golang using GORM, what I want to do is to execute the following query

SELECT * FROM Users u INNER JOIN Jobposition j ON j.id = u.JobpositionID

I would appreciate any help. Greetings

    
asked by Edgar Conrado 24.02.2018 в 19:50
source

1 answer

1

You can use Raw SQL (documentation in link ). I leave an example of the code:

var result Result
db.Raw("SELECT name,email,password,jobpositionid,createdat,updatedat,deleteat FROM Users u INNER JOIN Jobposition j ON j.id = u.JobpositionID", 3).Scan(&result)
    
answered by 31.07.2018 / 04:00
source