gorm/utils/tests/models.go

61 lines
1.2 KiB
Go
Raw Normal View History

package tests
import (
"database/sql"
"time"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
)
// 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)
type User struct {
gorm.Model
Name string
Age uint
Birthday *time.Time
Account Account
Pets []*Pet
Toys []Toy `gorm:"polymorphic:Owner"`
CompanyID *int
Company Company
2020-03-12 03:39:42 +03:00
ManagerID *uint
Manager *User
2020-02-01 16:48:06 +03:00
Team []User `gorm:"foreignkey:ManagerID"`
Languages []Language `gorm:"many2many:UserSpeak"`
Friends []*User `gorm:"many2many:user_friends"`
2020-02-15 14:45:27 +03:00
Active bool
}
type Account struct {
gorm.Model
UserID sql.NullInt64
Number string
}
type Pet struct {
gorm.Model
UserID uint
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}
type Toy struct {
gorm.Model
2020-04-17 03:40:07 +03:00
Name string
OwnerID string
OwnerType string
}
type Company struct {
2020-02-22 18:08:20 +03:00
ID int
Name string
}
type Language struct {
Code string `gorm:"primarykey"`
Name string
}