Update README
This commit is contained in:
parent
45cfaf3466
commit
fcf25c48c4
@ -5,7 +5,7 @@
|
|||||||
## Belongs To
|
## Belongs To
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// User belongs to a profile, ProfileID is the foreign key
|
// `User` belongs to `Profile`, `ProfileID` is the foreign key
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Profile Profile
|
Profile Profile
|
||||||
@ -14,13 +14,44 @@ type User struct {
|
|||||||
|
|
||||||
type Profile struct {
|
type Profile struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Model(&user).Related(&profile)
|
db.Model(&user).Related(&profile)
|
||||||
//// SELECT * FROM profiles WHERE id = 111; // 111 is user's foreign key ProfileID
|
//// SELECT * FROM profiles WHERE id = 111; // 111 is user's foreign key ProfileID
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Profile Profile `gorm:"ForeignKey:ProfileRefer"` // use ProfileRefer as foreign key
|
||||||
|
ProfileRefer int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key & Association Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Refer string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Profile Profile `gorm:"ForeignKey:ProfileID;AssociationForeignKey:Refer"`
|
||||||
|
ProfileID int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Has One
|
## Has One
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -44,6 +75,37 @@ db.Model(&user).Related(&card, "CreditCard")
|
|||||||
db.Model(&user).Related(&card)
|
db.Model(&user).Related(&card)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
UserRefer uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Profile Profile `gorm:"ForeignKey:UserRefer"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key & Association Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
UserID uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Refer string
|
||||||
|
Profile Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Has Many
|
## Has Many
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -63,6 +125,37 @@ db.Model(&user).Related(&emails)
|
|||||||
//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key
|
//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
UserRefer uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Profile []Profiles `gorm:"ForeignKey:UserRefer"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Specify Foreign Key & Association Key*
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Profile struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
UserID uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Refer string
|
||||||
|
Profile []Profiles `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Many To Many
|
## Many To Many
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Loading…
x
Reference in New Issue
Block a user