github.com/systematiccaos/gorm@v1.22.6/utils/tests/models.go (about) 1 package tests 2 3 import ( 4 "database/sql" 5 "time" 6 7 "github.com/systematiccaos/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 `Pets` (has many) 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 CompanyID *int 25 Company Company 26 ManagerID *uint 27 Manager *User 28 Team []User `gorm:"foreignkey:ManagerID"` 29 Languages []Language `gorm:"many2many:UserSpeak;"` 30 Friends []*User `gorm:"many2many:user_friends;"` 31 Active bool 32 } 33 34 type Account struct { 35 gorm.Model 36 UserID sql.NullInt64 37 Number string 38 } 39 40 type Pet struct { 41 gorm.Model 42 UserID *uint 43 Name string 44 Toy Toy `gorm:"polymorphic:Owner;"` 45 } 46 47 type Toy struct { 48 gorm.Model 49 Name string 50 OwnerID string 51 OwnerType string 52 } 53 54 type Company struct { 55 ID int 56 Name string 57 } 58 59 type Language struct { 60 Code string `gorm:"primarykey"` 61 Name string 62 } 63 64 type Coupon struct { 65 ID int `gorm:"primarykey; size:255"` 66 AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"` 67 AmountOff uint32 `gorm:"amount_off"` 68 PercentOff float32 `gorm:"percent_off"` 69 } 70 71 type CouponProduct struct { 72 CouponId int `gorm:"primarykey;size:255"` 73 ProductId string `gorm:"primarykey;size:255"` 74 Desc string 75 } 76 77 type Order struct { 78 gorm.Model 79 Num string 80 Coupon *Coupon 81 CouponID string 82 }