gorm/doc/associations/belongs-to.md
2016-02-24 23:30:44 +08:00

19 lines
325 B
Markdown

# Belongs To
```go
// User belongs to a profile, ProfileID is the foreign key
type User struct {
gorm.Model
Profile Profile
ProfileID int
}
type Profile struct {
gorm.Model
Name string
}
db.Model(&user).Related(&profile)
//// SELECT * FROM profiles WHERE id = 111; // 111 is user's foreign key ProfileID
```