I'm a newbie for GOlang and GORM, I have some confusion how to multiple table join using GORM.
Example:
Tables:
Department - Fields (gorm.Modal, dep_name)
Employee - Fields (gorm.Modal, emp_id, emp_name, department_id) //employee is department table child
EmployeeContact - Fields (gorm.Modal, employee_id, emp_contact_no)//Employee contact table is employee table child
Query
SELECT * FROM department d, employee e, employeeContact ec WHERE d.id = e.department_id and e.id = ec.employee_id
How to make the above query using GORM?
I was looking for the solution here but since I already figured it out by myself, I would like to post it here also. My query was a bit different and I was joining only 2 tables but I think this one should work, too.
if err := db.Table("employee").Select("department.id, employee.department_id, employeeContact.employee_id").Joins("JOIN department on department.id = employee.department_id").Joins("JOIN employeeContact on employeeContact.id = employee.id").Find(&results).Error; err != nil {
return err, ""
}