-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrong foreignKey setup for multiple ebedded belongs-to relations
- Loading branch information
Showing
4 changed files
with
48 additions
and
72 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 |
---|---|---|
@@ -1,60 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
// User has one `Account` (has one), many `Pets` (has many) and `Toys` (has many - polymorphic) | ||
// He works in a Company (belongs to), he has a Manager (belongs to - single-table), and also managed a Team (has many - single-table) | ||
// He speaks many languages (many to many) and has many friends (many to many - single-table) | ||
// His pet also has one Toy (has one - polymorphic) | ||
type User struct { | ||
gorm.Model | ||
Name string | ||
Age uint | ||
Birthday *time.Time | ||
Account Account | ||
Pets []*Pet | ||
Toys []Toy `gorm:"polymorphic:Owner"` | ||
CompanyID *int | ||
Company Company | ||
ManagerID *uint | ||
Manager *User | ||
Team []User `gorm:"foreignkey:ManagerID"` | ||
Languages []Language `gorm:"many2many:UserSpeak"` | ||
Friends []*User `gorm:"many2many:user_friends"` | ||
Active bool | ||
} | ||
|
||
type Account struct { | ||
gorm.Model | ||
UserID sql.NullInt64 | ||
Number string | ||
} | ||
|
||
type Pet struct { | ||
gorm.Model | ||
UserID *uint | ||
Name string | ||
Toy Toy `gorm:"polymorphic:Owner;"` | ||
} | ||
|
||
type Toy struct { | ||
gorm.Model | ||
Name string | ||
OwnerID string | ||
OwnerType string | ||
type Country struct { | ||
Name string `gorm:"primaryKey"` | ||
} | ||
|
||
type Company struct { | ||
ID int | ||
Name string | ||
type Address struct { | ||
CountryName string | ||
Country Country | ||
} | ||
|
||
type Language struct { | ||
Code string `gorm:"primarykey"` | ||
Name string | ||
type Org struct { | ||
ID int | ||
Address1 Address `gorm:"embedded;embeddedPrefix:address1_"` | ||
Address2 Address `gorm:"embedded;embeddedPrefix:address2_"` | ||
} |