I am new to ORM (and GORM) so apologies if this is an obvious question, but it does not seem to be covered by the documentation.
I will be using the examples from the documentation as a base to my questions
Question 1: Belongs To
// `User` belongs to `Company`, `CompanyID` is the foreign key
type User struct {
gorm.Model
Name string
CompanyID int
Company Company
}
type Company struct {
ID int
Name string
}
- A
Userbelongs to oneCompanyonly → this is handled by the code above - A
Companyhas manyUser→ is this implied by the code above? Or should I add somehow a relation O2M inCompany?
Question 2: Has Many
// User has many CreditCards, UserID is the foreign key
type User struct {
gorm.Model
CreditCards []CreditCard
}
type CreditCard struct {
gorm.Model
Number string
UserID uint
}
- A
Userhas 1+CreditCard→ this is handled by the code - A
CreditCardcan belong to several users (say, a shared family CC) → is it implied? (if not: how to set up the O2M relationship).
Or is it, instead, a case where aCreditCardis explicitly configured to belong to only one user?