associations

package
v0.0.0-...-9ad72aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateUser2

func CreateUser2(db *gorm.DB)

func FindUserPreloadOrders

func FindUserPreloadOrders(db *gorm.DB)

Types

type Address

type Address struct {
	gorm.Model
	Name string
}

type Cat

type Cat struct {
	gorm.Model
	Name string
	Toy  Toy `gorm:"polymorphism:Owner;polymorphicValue:cat_toy;"`
}

type Company

type Company struct {
	// ID int
	gorm.Model
	CompanyRefer int
	Code         string
	Name         string
}

type CreditCard

type CreditCard struct {
	gorm.Model
	Number   string
	UserID   uint
	UserName string
}

CreditCard belongs to UserModel, UserID is the foreign key

type CreditCardM

type CreditCardM struct {
	gorm.Model
	Number     string
	UserID     uint
	UserRefer  uint
	UserNumber string
}

type Dog

type Dog struct {
	gorm.Model
	Name string
	Toy  Toy `gorm:"polymorphism:Owner;"`
}

type DogM

type DogM struct {
	gorm.Model
	Toys []ToyM `gorm:"polymorphic:Owner;"`
}

type Language

type Language struct {
	gorm.Model
	Name string
}

func RetrieveLanguagesWithUsersEagerLoading

func RetrieveLanguagesWithUsersEagerLoading(db *gorm.DB) ([]Language, error)

type LanguageOverrideForeignKey

type LanguageOverrideForeignKey struct {
	gorm.Model
	Name  string
	Refer uint `gorm:"index:;unique"`
}

type LanguagesBackReference

type LanguagesBackReference struct {
	gorm.Model
	Name  string
	Users []*UserBackReference `gorm:"many2many:user_languages;"`
}

type Order

type Order struct {
	gorm.Model
	UserID uint
	Price  float64
}

type Person

type Person struct {
	gorm.Model
	Name      string
	Addresses []Address `gorm:"many2many:person_addresses"`
}

type PersonAddress

type PersonAddress struct {
	PersonID  uint `gorm:"primaryKey"`
	AddressID uint `gorm:"primaryKey"`
	CreatedAt time.Time
	DeletedAt gorm.DeletedAt
}

func RetrieveJoinTables

func RetrieveJoinTables(db *gorm.DB) ([]PersonAddress, error)

type Toy

type Toy struct {
	gorm.Model
	Name      string
	OwnerID   uint
	OwnerType string
}

type ToyM

type ToyM struct {
	gorm.Model
	OwnerID   uint
	OwnerType string
}

type User

type User struct {
	gorm.Model
	Name      string
	CompanyID int
	Company   Company
}

User belongs to Company, CompanyID is the foreign key. There is both a CompanyID and a Company. By default, the CompanyID is implicitly used to create a foreign key relationship between the User and Company tables, and thus must be included in the User struct in order to fill the Company inner struct.

type User2

type User2 struct {
	gorm.Model
	Name      string
	Languages []Language `gorm:"many2many:user_languages;"`
}

User2 has and belongs to many languages, `user_languages` is the join table When using GORM AutoMigrate to create a table for User, GORM will create the join table automatically.

func RetrieveUsersWithLanguagesEagerLoading

func RetrieveUsersWithLanguagesEagerLoading(db *gorm.DB) ([]User2, error)

type User2OverrideForeignKey

type User2OverrideForeignKey struct {
	gorm.Model
	Languages []LanguageOverrideForeignKey `gorm:"many2many:user_languages;foreignKey:Refer;joinForeignKey:UserReferID;references:Refer;joinReferences:ProfileReferID"`
	Refer     uint                         `gorm:"index:;unique"`
}

type User3

type User3 struct {
	gorm.Model
	Name   string
	Orders []Order
}

type UserBackReference

type UserBackReference struct {
	gorm.Model
	Languages []*LanguagesBackReference `gorm:"many2many:user_languages;"`
}

type UserForeignKeyConstrains

type UserForeignKeyConstrains struct {
	gorm.Model
	CreditCard CreditCard `gorm:"constrains:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

type UserForeignKeyConstraints

type UserForeignKeyConstraints struct {
	gorm.Model
	Name      string
	CompanyID int
	Company   Company `gorm:"constraints:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

type UserM

type UserM struct {
	gorm.Model
	CreditCards []CreditCard
}

UserM has many CreditCards, UserID is the foreign key

func GetAll

func GetAll(db *gorm.DB) ([]UserM, error)

GetAll Retrieve user list with eager loading credit cards

type UserMOverrideForeignKey

type UserMOverrideForeignKey struct {
	gorm.Model
	// CreditCardM.UserRefer = User.ID
	CreditCards []CreditCardM `gorm:"foreignKey:UserRefer"`
}

type UserMOverrideReferences

type UserMOverrideReferences struct {
	gorm.Model
	MemberNumber string
	// CreditCard.UserNumber = User.MemberNumber
	CreditCards []CreditCardM `gorm:"foreignKey:UserNumber;references=MemberNumber"`
}

type UserMSelfReferentialHasMany

type UserMSelfReferentialHasMany struct {
	gorm.Model
	Name      string
	ManagerID *uint
	Teammates []UserMSelfReferentialHasMany `gorm:"foreignKey:ManagerID"`
}

type UserModel

type UserModel struct {
	gorm.Model
	CreditCard CreditCard
}

UserModel has one CreditCard, UserID is the foreign key

func GetAllUsers

func GetAllUsers(db *gorm.DB) ([]UserModel, error)

type UserModelOverrideForeignKey

type UserModelOverrideForeignKey struct {
	gorm.Model
	CreditCard CreditCard `gorm:"foreignKey:UserName"`
}

UserModelOverrideForeignKey For a has one relationship, a foreign key field must also exist, the owner will save the primary key of the model belongs to it into this field.

type UserModelOverrideReferences

type UserModelOverrideReferences struct {
	gorm.Model
	Name       string     `gorm:"index"`
	CreditCard CreditCard `gorm:"foreignKey:UserName;references:name"`
}

UserModelOverrideReferences When save User model, save User.name to CreditCard.UserName as to set up the foreign key. By default, the owned entity will save the has one model's primary key into a foreign key.

type UserOverrideForeignKey

type UserOverrideForeignKey struct {
	gorm.Model
	Name         string
	CompanyRefer int
	Company      Company `gorm:"foreignKey:CompanyRefer"` // use CompanyRefer as foreign key
}

type UserOverrideReferences

type UserOverrideReferences struct {
	gorm.Model
	Name       string
	CompanyKey string
	Company    Company `gorm:"references:Code"` // assign Company.Code to User.CompanyKey
}

type UserSelfReferentialHasOne

type UserSelfReferentialHasOne struct {
	gorm.Model
	Name      string
	ManagerID *uint
	Manager   *UserSelfReferentialHasOne
}

type UserSelfReferentialMany2Many

type UserSelfReferentialMany2Many struct {
	gorm.Model
	Friends []*User `gorm:"many2many:user_friends;"`
}

type UserSpecifyReference

type UserSpecifyReference struct {
	gorm.Model
	Name      string
	CompanyID string
	// Company already exists CompanyID field, we need to specify the references field
	// to keep association to be `belongs to`, otherwise, GORM will guess it as `has one`
	Company Company `gorm:"references:CompanyID"`
}

UserSpecifyReference GORM usually guess the relationship as `has one` if override foreign key name already exists in owner's type, we need to specify references in the belongs to relationship.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL