forked from mirror/gorm
Add README.md updates for polymorphism.
This commit is contained in:
parent
794e1ba20b
commit
7e8c0f7edd
27
README.md
27
README.md
|
@ -17,6 +17,7 @@ The fantastic ORM library for Golang, aims to be developer friendly.
|
||||||
* Iteration Support via [Rows](#row--rows)
|
* Iteration Support via [Rows](#row--rows)
|
||||||
* Scopes
|
* Scopes
|
||||||
* sql.Scanner support
|
* sql.Scanner support
|
||||||
|
* Polymorphism
|
||||||
* Every feature comes with tests
|
* Every feature comes with tests
|
||||||
* Convention Over Configuration
|
* Convention Over Configuration
|
||||||
* Developer Friendly
|
* Developer Friendly
|
||||||
|
@ -507,6 +508,32 @@ db.Model(&user).Association("Languages").Clear()
|
||||||
// Remove all relations between the user and languages
|
// Remove all relations between the user and languages
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Polymorphism
|
||||||
|
|
||||||
|
Supports polymorphic has-many and has-one associations.
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Cat struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
Toy Toy `gorm:"polymorphic:Owner;"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Dog struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
Toy Toy `gorm:"polymorphic:Owner;"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Toy struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
OwnerId int
|
||||||
|
OwnerType int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Note: polymorphic belongs-to and many-to-many are explicitly NOT supported, and will throw errors.
|
||||||
|
|
||||||
## Advanced Usage
|
## Advanced Usage
|
||||||
|
|
||||||
## FirstOrInit
|
## FirstOrInit
|
||||||
|
|
Loading…
Reference in New Issue