err := bd.Model(userModel). Relation("Partner"). Relation("AccountSecurity"). Relation("UserRole"). Relation("UserRole.Role"). <- nested relation Scan(s.ctx) type UserModel struct { bun.BaseModel `bun:"table:user"` ID string `json:"id" bun:"id,pk,default:gen_random_uuid()"` CreatedTime time.Time `json:"createdTime" bun:"created_time,notnull,default:current_timestamp"` UpdatedTime null.Time `json:"updatedTime" bun:"updated_time,nullzero"` DeletedTime null.Time `json:"deletedTime" bun:"deleted_time,nullzero"` Name string `json:"name" bun:"name,notnull"` Email string `json:"email" bun:"email,notnull,unique"` Mobile string `json:"mobile" bun:"mobile,notnull,unique"` Status string `json:"status" bun:"status,notnull,default:inactive"` Verified null.Bool `json:"verified" bun:"verified,notnull,default:false"` AvatarFileID null.String `json:"avatarFileId" bun:"avatar_file_id,nullzero"` PartnerID string `json:"partnerId" bun:"partner_id,notnull"` Partner PartnerRelation `bun:"rel:belongs-to,join:partner_id=id"` AccountSecurity AccountSecurityRelation `bun:"rel:has-one,join:id=user_id"` UserRole UserRoleRelation `bun:"rel:has-one,join:id=user_id"` } /* ================================================ USER RELATION TERITORY ================================================ */ type PartnerRelation struct { bun.BaseModel `bun:"table:partner"` ID string `json:"id" bun:"id,pk"` Name string `json:"name" bun:"name"` Code string `json:"code" bun:"code"` } type AccountSecurityRelation struct { bun.BaseModel `bun:"table:account_security"` ID string `json:"id" bun:"id,pk"` UserID string `json:"userId" bun:"user_id"` PasswordEnabled bool `json:"name" bun:"password_enabled"` PasswordSecret string `json:"code" bun:"password_secret"` } type UserRoleRelation struct { bun.BaseModel `bun:"table:user_role"` ID string `json:"id" bun:"id,pk"` RoleID string `json:"roleId" bun:"role_id"` UserID string `json:"userId" bun:"user_id"` Role RoleRelation `bun:"rel:belongs-to,join:role_id=id"` } type RoleRelation struct { bun.BaseModel `bun:"table:role"` ID string `json:"id" bun:"id,pk"` Name string `json:"name" bun:"name"` Status string `json:"status" bun:"status"` }