2020-02-01 15:18:25 +03:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
2020-02-01 15:18:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// User has one `Account` (has one), many `Pets` (has many) and `Toys` (has many - polymorphic)
|
|
|
|
// He works in a Company (belongs to), he has a Manager (belongs to - single-table), and also managed a Team (has many - single-table)
|
|
|
|
// He speaks many languages (many to many) and has many friends (many to many - single-table)
|
|
|
|
// His pet also has one Toy (has one - polymorphic)
|
2021-07-17 16:45:15 +03:00
|
|
|
// NamedPet is a reference to a Named `Pets` (has many)
|
2020-02-01 15:18:25 +03:00
|
|
|
type User struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
Age uint
|
|
|
|
Birthday *time.Time
|
|
|
|
Account Account
|
|
|
|
Pets []*Pet
|
2021-07-17 16:45:15 +03:00
|
|
|
NamedPet *Pet
|
2020-02-01 15:18:25 +03:00
|
|
|
Toys []Toy `gorm:"polymorphic:Owner"`
|
|
|
|
CompanyID *int
|
|
|
|
Company Company
|
2020-03-12 03:39:42 +03:00
|
|
|
ManagerID *uint
|
2020-02-01 15:18:25 +03:00
|
|
|
Manager *User
|
2020-02-01 16:48:06 +03:00
|
|
|
Team []User `gorm:"foreignkey:ManagerID"`
|
2020-07-08 12:59:40 +03:00
|
|
|
Languages []Language `gorm:"many2many:UserSpeak;"`
|
|
|
|
Friends []*User `gorm:"many2many:user_friends;"`
|
2020-02-15 14:45:27 +03:00
|
|
|
Active bool
|
2020-02-01 15:18:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
gorm.Model
|
|
|
|
UserID sql.NullInt64
|
|
|
|
Number string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pet struct {
|
|
|
|
gorm.Model
|
2020-06-19 19:48:15 +03:00
|
|
|
UserID *uint
|
2020-02-01 15:18:25 +03:00
|
|
|
Name string
|
|
|
|
Toy Toy `gorm:"polymorphic:Owner;"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Toy struct {
|
|
|
|
gorm.Model
|
2020-04-17 03:40:07 +03:00
|
|
|
Name string
|
2020-02-01 15:18:25 +03:00
|
|
|
OwnerID string
|
|
|
|
OwnerType string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Company struct {
|
2020-02-22 18:08:20 +03:00
|
|
|
ID int
|
2020-02-01 15:18:25 +03:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Language struct {
|
2020-02-01 19:03:56 +03:00
|
|
|
Code string `gorm:"primarykey"`
|
2020-02-01 15:18:25 +03:00
|
|
|
Name string
|
|
|
|
}
|
2021-11-29 06:02:32 +03:00
|
|
|
|
|
|
|
type Coupon struct {
|
2021-12-02 05:20:16 +03:00
|
|
|
ID int `gorm:"primarykey; size:255"`
|
2021-11-29 06:02:32 +03:00
|
|
|
AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"`
|
|
|
|
AmountOff uint32 `gorm:"amount_off"`
|
|
|
|
PercentOff float32 `gorm:"percent_off"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CouponProduct struct {
|
2021-12-02 05:20:16 +03:00
|
|
|
CouponId int `gorm:"primarykey;size:255"`
|
|
|
|
ProductId string `gorm:"primarykey;size:255"`
|
|
|
|
Desc string
|
2021-11-29 06:02:32 +03:00
|
|
|
}
|
2021-11-29 13:34:50 +03:00
|
|
|
|
|
|
|
type Order struct {
|
|
|
|
gorm.Model
|
|
|
|
Num string
|
|
|
|
Coupon *Coupon
|
|
|
|
CouponID string
|
|
|
|
}
|