| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"time" |
| 6 |
|
| 7 |
"gorm.io/gorm" |
| 8 |
) |
| 9 |
|
| 10 |
// User has one `Account` (has one), many `Pets` (has many) and `Toys` (has many - polymorphic) |
| 11 |
// He works in a Company (belongs to), he has a Manager (belongs to - single-table), and also managed a Team (has many - single-table) |
| 12 |
// He speaks many languages (many to many) and has many friends (many to many - single-table) |
| 13 |
// His pet also has one Toy (has one - polymorphic) |
| 14 |
// NamedPet is a reference to a named `Pet` (has one) |
| 15 |
type User struct { |
| 16 |
gorm.Model |
| 17 |
Name string |
| 18 |
Age uint |
| 19 |
Birthday *time.Time |
| 20 |
Account Account |
| 21 |
Pets []*Pet |
| 22 |
NamedPet *Pet |
| 23 |
Toys []Toy `gorm:"polymorphic:Owner"` |
| 24 |
Tools []Tools `gorm:"polymorphicType:Type;polymorphicId:CustomID"` |
| 25 |
CompanyID *int |
| 26 |
Company Company |
| 27 |
ManagerID *uint |
| 28 |
Manager *User |
| 29 |
Team []User `gorm:"foreignkey:ManagerID"` |
| 30 |
Languages []Language `gorm:"many2many:UserSpeak;"` |
| 31 |
Friends []*User `gorm:"many2many:user_friends;"` |
| 32 |
Active bool |
| 33 |
} |
| 34 |
|
| 35 |
type Account struct { |
| 36 |
gorm.Model |
| 37 |
UserID sql.NullInt64 |
| 38 |
Number string |
| 39 |
} |
| 40 |
|
| 41 |
type Pet struct { |
| 42 |
gorm.Model |
| 43 |
UserID *uint |
| 44 |
Name string |
| 45 |
Toy Toy `gorm:"polymorphic:Owner;"` |
| 46 |
} |
| 47 |
|
| 48 |
type Toy struct { |
| 49 |
gorm.Model |
| 50 |
Name string |
| 51 |
OwnerID string |
| 52 |
OwnerType string |
| 53 |
} |
| 54 |
|
| 55 |
type Tools struct { |
| 56 |
gorm.Model |
| 57 |
Name string |
| 58 |
CustomID string |
| 59 |
Type string |
| 60 |
} |
| 61 |
|
| 62 |
type Company struct { |
| 63 |
ID int |
| 64 |
Name string |
| 65 |
} |
| 66 |
|
| 67 |
type Language struct { |
| 68 |
Code string `gorm:"primarykey"` |
| 69 |
Name string |
| 70 |
} |
| 71 |
|
| 72 |
type Coupon struct { |
| 73 |
ID int `gorm:"primarykey; size:255"` |
| 74 |
AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"` |
| 75 |
AmountOff uint32 `gorm:"column:amount_off"` |
| 76 |
PercentOff float32 `gorm:"column:percent_off"` |
| 77 |
} |
| 78 |
|
| 79 |
type CouponProduct struct { |
| 80 |
CouponId int `gorm:"primarykey;size:255"` |
| 81 |
ProductId string `gorm:"primarykey;size:255"` |
| 82 |
Desc string |
| 83 |
} |
| 84 |
|
| 85 |
type Order struct { |
| 86 |
gorm.Model |
| 87 |
Num string |
| 88 |
Coupon *Coupon |
| 89 |
CouponID string |
| 90 |
} |
| 91 |
|
| 92 |
type Parent struct { |
| 93 |
gorm.Model |
| 94 |
FavChildID uint |
| 95 |
FavChild *Child |
| 96 |
Children []*Child |
| 97 |
} |
| 98 |
|
| 99 |
type Child struct { |
| 100 |
gorm.Model |
| 101 |
Name string |
| 102 |
ParentID *uint |
| 103 |
Parent *Parent |
| 104 |
} |