mirror of https://github.com/go-gorm/gorm.git
Update Document For Related()
This commit is contained in:
parent
98869cb124
commit
2f5991d088
|
@ -102,6 +102,8 @@ db.Save(&user)
|
||||||
|
|
||||||
### Create With SubStruct
|
### Create With SubStruct
|
||||||
|
|
||||||
|
Refer [Query With Related](#query-with-related) to find how to find associations
|
||||||
|
|
||||||
```go
|
```go
|
||||||
user := User{
|
user := User{
|
||||||
Name: "jinzhu",
|
Name: "jinzhu",
|
||||||
|
@ -260,12 +262,15 @@ db.Where("name = 'jinzhu'").Or(map[string]interface{}{"name": "jinzhu 2"}).Find(
|
||||||
### Query With Related
|
### Query With Related
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// Find emails from user with guessed foreign key
|
||||||
db.Model(&user).Related(&emails)
|
db.Model(&user).Related(&emails)
|
||||||
//// SELECT * FROM emails WHERE user_id = 111;
|
//// SELECT * FROM emails WHERE user_id = 111;
|
||||||
|
|
||||||
|
// Find address from user with specified foreign key 'BillingAddressId'
|
||||||
db.Model(&user).Related(&address1, "BillingAddressId")
|
db.Model(&user).Related(&address1, "BillingAddressId")
|
||||||
//// SELECT * FROM addresses WHERE id = 123; // 123 is the value of user's BillingAddressId
|
//// SELECT * FROM addresses WHERE id = 123; // 123 is the value of user's BillingAddressId
|
||||||
|
|
||||||
|
// Find user from email with guessed primary key value from emails
|
||||||
db.Model(&email).Related(&user)
|
db.Model(&email).Related(&user)
|
||||||
//// SELECT * FROM users WHERE id = 111; // 111 is the value of email's UserId
|
//// SELECT * FROM users WHERE id = 111; // 111 is the value of email's UserId
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue