I have those two models:
User model:
type User struct {
DBBase
Email string `gorm:"column:email" json:"email"`
Password string `gorm:"column:password" json:"-"`
}
func (User) TableName() string {
return "t_user"
}
User info model:
type UserInfo struct {
User User `gorm:"foreignkey:u_id;association_foreignkey:id"`
UID uint `gorm:"column:u_id" json:"-"`
FirstName string `gorm:"column:first_name" json:"first_name"`
LastName string `gorm:"column:last_name" json:"last_name"`
Phone string `gorm:"column:phone" json:"phone"`
Address string `gorm:"column:address" json:"address"`
}
func (UserInfo) TableName() string {
return "t_user_info"
}
and I want to make UID related to the id of the user table.
this is the function that creates the user:
func (dao *AuthDAO) Register(rs app.RequestScope, user *models.User, userInfo *models.UserInfo) (userErr error, userInfoErr error) {
createUser := rs.Db().Create(&user)
userInfo.UID = user.ID
createUserInfo := rs.Db().Create(&userInfo)
return createUser.Error, createUserInfo.Error
}
I did try what gorm wrote on the documentation, but without success: http://doc.gorm.io/associations.html
gorm:"foreignkey:UID"
? How are you trying to retrieve the relationship? Any errors? – Henrion