Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEMO: failed to guess relations for embedded types #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func OpenTestConnection() (db *gorm.DB, err error) {

func RunMigrations() {
var err error
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
allModels := []interface{}{&User{}, &Document{}, &DocumentFulltext{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
79 changes: 75 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,84 @@ import (
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
func TestCreateData(t *testing.T) {

DB.Create(&user)
users, _, _ := createSomeTestdata()

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
if err := DB.First(&result, users[0].ID).Error; err != nil {

t.Errorf("Failed, got error: %v", err)
}
}

func TestReadData(t *testing.T) {

users, _, _ := createSomeTestdata()

queryConnection := DB
queryConnection = queryConnection.Joins("left join document_fulltexts on documents.id = document_fulltexts.document_id")
queryConnection = queryConnection.Where(`"documents"."user_id" = ?`, users[0].ID).Order(`created_at desc`)

queryConnection = queryConnection.Select(
`"documents"."user_id",
"documents"."name",
CASE WHEN "document_fulltexts"."fulltext" IS NOT NULL THEN TRUE ELSE FALSE END AS "fulltext_exists"`,
)

documents := []DocumentListEntry{}

queryConnection.Table("documents").Scan(&documents)

if len(documents) != 3 {
t.Errorf("Failed to get all documents from the DB")
}
}

func createSomeTestdata() ([]User, []Document, []DocumentFulltext) {

var users []User
var documents []Document
var documentFulltexts []DocumentFulltext

user1 := User{Name: "user1"}

DB.Create(&user1)
document1 := Document{
UserID: user1.ID,
Name: "document1",
}
DB.Create(&document1)

document2 := Document{
UserID: user1.ID,
Name: "document2",
}
DB.Create(&document2)

document3 := Document{
UserID: user1.ID,
Name: "document3",
}
DB.Create(&document3)

documentFulltext1 := DocumentFulltext{
DocumentID: document1.ID,
Name: "documentFulltext1",
}
DB.Create(&documentFulltext1)
documentFulltext3 := DocumentFulltext{
DocumentID: document3.ID,
Name: "documentFulltext3",
}
DB.Create(&documentFulltext3)

users = append(users, user1)
documents = append(documents, document1)
documents = append(documents, document2)
documents = append(documents, document3)
documentFulltexts = append(documentFulltexts, documentFulltext1)
documentFulltexts = append(documentFulltexts, documentFulltext3)

return users, documents, documentFulltexts
}
57 changes: 13 additions & 44 deletions models.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,29 @@
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
Documents []*Document // has-many
}

type Account struct {
type Document struct {
gorm.Model
UserID sql.NullInt64
Number string
User *User // belongs-to
UserID uint
Name string
DocumentFulltext *DocumentFulltext // has-one
}

type Pet struct {
type DocumentFulltext struct {
gorm.Model
UserID *uint
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
gorm.Model
Name string
OwnerID string
OwnerType string
}

type Company struct {
ID int
Name string
DocumentID uint
Document *Document // belongs-to
Name string
}

type Language struct {
Code string `gorm:"primarykey"`
Name string
type DocumentListEntry struct {
Document `gorm:"embedded"`
FulltextExists bool
}