无法按预期工作的 GORM 关联

huangapple go评论78阅读模式
英文:

Can't get GORM associations to work as expected

问题

我的两个模型是:

package models

// Business ...
type Business struct {
    ID     uint
    Name   string `gorm:"not null"`
    Tables Tables `gorm:"ForeignKey:BusinessID"`
}

// Businesses ...
type Businesses []Business

package models

// Table ...
type Table struct {
    ID         uint
    Ref        string `gorm:"not null"`
    Business   Business
    BusinessID uint
}

// Tables ...
type Tables []Table

从代码中可以看出,关联关系应该是一个“business”拥有多个“tables”,而一个“table”属于一个“business”。然而,当数据库被创建时,没有创建外键(我正在使用sqlite3),当我返回已创建的business时:

bus := models.Business{
    Name: "Test",
    Tables: models.Tables{
        models.Table{Ref: "A1"},
    },
}
db.Create(&bus)

businesses数组是空的,当返回table时,虽然business_id是正确的,但business结构也是空的。

英文:

My two models are

package models

// Business ...
type Business struct {
	ID     uint
	Name   string `gorm:"not null"`
	Tables Tables `gorm:"ForeignKey:BusinessID"`
}

// Businesses ...
type Businesses []Business

and

package models

// Table ...
type Table struct {
	ID         uint
	Ref        string `gorm:"not null"`
	Business   Business
	BusinessID uint
}

// Tables ...
type Tables []Table

It may be obvious from the code, but the association should be that one 'business' has many 'tables' and a 'table' belong to a 'business'. However, when the database is created there are no foreign keys created (I am using sqlite3) and when I return the business which has been created with

 bus := models.Business{
		Name: "Test",
		Tables: models.Tables{
		models.Table{Ref: "A1"},
	},
}
db.Create(&bus)

the businesses array is empty, and when the table is returned although the business_id is correct, there is business struct is empty also.

答案1

得分: 2

我无法重现你的问题。我这里有一个可行的解决方案。我怀疑在一个单独的模型包中使用实体可能不起作用,但实际上也可以正常工作。

package main

import (
	"log"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	_ "github.com/mattn/go-sqlite3"
)

type Business struct {
	ID     uint
	Name   string `gorm:"not null"`
	Tables Tables `gorm:"ForeignKey:BusinessID"`
}

type Table struct {
	ID         uint
	Ref        string `gorm:"not null"`
	Business   Business
	BusinessID uint
}

type Tables []Table
type Businesses []Business

func main() {
	var err error
	var db *gorm.DB

	db, err = gorm.Open("sqlite3", "test.db")
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	db.LogMode(true)
	db.AutoMigrate(&Business{})
	db.AutoMigrate(&Table{})

	bus := Business{
		Name: "Test",
		Tables: Tables{
			Table{Ref: "A1"},
		},
	}
	db.Create(&bus)
	var businesses Businesses
	db.Preload("Tables").Find(&businesses)
	log.Println(businesses)
}
英文:

I could not reproduce your problem. I have a working solution here. I suspected that it would not work with the entities in a separate models package, but that worked as well.

package main

import (
	"log"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	_ "github.com/mattn/go-sqlite3"
)

type Business struct {
	ID     uint
	Name   string `gorm:"not null"`
	Tables Tables `gorm:"ForeignKey:BusinessID"`
}

type Table struct {
	ID         uint
	Ref        string `gorm:"not null"`
	Business   Business
	BusinessID uint
}

type Tables []Table
type Businesses []Business

func main() {
	var err error
	var db *gorm.DB

	db, err = gorm.Open("sqlite3", "test.db")
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	db.LogMode(true)
	db.AutoMigrate(&Business{})
	db.AutoMigrate(&Table{})

	bus := Business{
		Name: "Test",
		Tables: Tables{
			Table{Ref: "A1"},
		},
	}
	db.Create(&bus)
	var businesses Businesses
	db.Preload("Tables").Find(&businesses)
	log.Println(businesses)
}

huangapple
  • 本文由 发表于 2017年5月29日 18:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/44240133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定