-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacto: add polymorphism informations (#759)
* refacto: add polymorphism informations * fix: doc
- Loading branch information
1 parent
1a09ba8
commit 461feed
Showing
5 changed files
with
60 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: Polymorphism | ||
layout: Page | ||
--- | ||
|
||
## Polymorphism Association | ||
|
||
GORM supports polymorphism association for `has one` and `has many`, it will save owned entity's table name into polymorphic type's field, primary key value into the polymorphic field | ||
|
||
By default `polymorphic:<value>` will prefix the column type and column id with `<value>`. | ||
The value will be the table name pluralized. | ||
|
||
```go | ||
type Dog struct { | ||
ID int | ||
Name string | ||
Toys []Toy `gorm:"polymorphic:Owner;"` | ||
} | ||
|
||
type Toy struct { | ||
ID int | ||
Name string | ||
OwnerID int | ||
OwnerType string | ||
} | ||
|
||
db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}}) | ||
// INSERT INTO `dogs` (`name`) VALUES ("dog1") | ||
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1",1,"dogs"), ("toy2",1,"dogs") | ||
``` | ||
|
||
|
||
You can specify polymorphism properties separately using the following GORM tags: | ||
|
||
- `polymorphicType`: Specifies the column type. | ||
- `polymorphicId`: Specifies the column ID. | ||
- `polymorphicValue`: Specifies the value of the type. | ||
|
||
```go | ||
type Dog struct { | ||
ID int | ||
Name string | ||
Toys Toy `gorm:"polymorphicType:Kind;polymorphicId:OwnerID;polymorphicValue:master"` | ||
} | ||
|
||
type Toy struct { | ||
ID int | ||
Name string | ||
OwnerID int | ||
Kind string | ||
} | ||
|
||
db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}}) | ||
// INSERT INTO `dogs` (`name`) VALUES ("dog1") | ||
// INSERT INTO `toys` (`name`,`owner_id`,`kind`) VALUES ("toy1",1,"master"), ("toy2",1,"master") | ||
``` | ||
|
||
In these examples, we've used a has-many relationship, but the same principles apply to has-one relationships. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters