无法使用gorm创建外键关系,其中字段名称为!= type name

时间:2016-04-10 00:34:58

标签: go go-gorm

我试图创建一个"属于"我的应用程序中的关系,其中字段的名称(和db列)与类型名称不同。 Gorm抱怨这个。这是一个演示问题的简短示例:

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/mattn/go-sqlite3"
)

type Car struct {
    gorm.Model
    Owner   User
    OwnerID int
}

type User struct {
    gorm.Model
}

func main() {
    db, _ := gorm.Open("sqlite3", "test.db")
    defer db.Close()
    db.LogMode(true)

    db.CreateTable(&Car{}, &User{})

    u := User{}
    u.ID = 1
    db.Create(&u)

    c := Car{}
    c.ID = 1
    c.Owner = u
    db.Create(&c)

    cars := make([]Car, 5)
    db.Find(&cars)
    for i := range cars {
        db.Model(cars[i]).Related(&cars[i].Owner)
    }

    for i := range cars {
        fmt.Println(cars[i].Owner.ID)
    }
}

我们希望这个程序打印出来" 1"作为最后一行,因为这是"拥有"的用户关系的主键。数据库中唯一的汽车。相反" 0"打印出来,详细日志记录在Related(&cars[i].Owner)步骤显示错误:

(/home/nate/gocode/src/foo/main.go:26)
[2016-04-09 19:33:46]  [122.35ms]  CREATE TABLE "cars" ("id" integer primary key autoincrement,"created_at" datetime,"updated_at" datetime,"deleted_at" datetime,"owner_id" integer )

(/home/nate/gocode/src/foo/main.go:26)
[2016-04-09 19:33:46]  [98.96ms]  CREATE INDEX idx_cars_deleted_at ON "cars"(deleted_at)

(/home/nate/gocode/src/foo/main.go:26)
[2016-04-09 19:33:46]  [108.95ms]  CREATE TABLE "users" ("id" integer primary key autoincrement,"created_at" datetime,"updated_at" datetime,"deleted_at" datetime )

(/home/nate/gocode/src/foo/main.go:26)
[2016-04-09 19:33:46]  [125.74ms]  CREATE INDEX idx_users_deleted_at ON "users"(deleted_at)

(/home/nate/gocode/src/foo/main.go:30)
[2016-04-09 19:33:46]  [0.53ms]  INSERT INTO "users" ("id","created_at","updated_at","deleted_at") VALUES ('1','2016-04-09T19:33:46-04:00','2016-04-09T19:33:46-04:00','<nil>')

(/home/nate/gocode/src/foo/main.go:35)
[2016-04-09 19:33:46]  [1.98ms]  UPDATE "users" SET "created_at" = '2016-04-09T19:33:46-04:00', "updated_at" = '2016-04-09T19:33:46-04:00', "deleted_at" = '<nil>'  WHERE "users".deleted_at IS NULL AND "users"."id" = '1'

(/home/nate/gocode/src/foo/main.go:35)
[2016-04-09 19:33:46]  [0.43ms]  INSERT INTO "cars" ("id","created_at","updated_at","deleted_at","owner_id") VALUES ('1','2016-04-09T19:33:46-04:00','2016-04-09T19:33:46-04:00','<nil>','1')

(/home/nate/gocode/src/foo/main.go:38)
[2016-04-09 19:33:46]  [0.64ms]  SELECT * FROM "cars"  WHERE "cars".deleted_at IS NULL

(/home/nate/gocode/src/foo/main.go:40)
[2016-04-09 19:33:46]  invalid association []
0

这里发生了什么?我必须在Owner字段上使用一些特殊标记吗?我尝试过使用gorm:"ForeignKey:owner_id"gorm:"AssociationForeignKey:owner_id",两者都给出了相同的结果。

1 个答案:

答案 0 :(得分:0)

在您的情况下,最简单的方法是指定外部字段名称。所以你的

from collections import defaultdict

dd = lambda: defaultdict(dd)
x = dd()
x['foo']['bar']['foobar'] = 1

应该像

db.Model(cars[i]).Related(&cars[i].Owner)

替代方式

  • db.Model(cars[i]).Related(&cars[i].Owner, "Owner") Owner字段重命名为OwnerIDUser
  • UserID结构重命名为User

在这些情况下,GORM会自动选择正确的外键。来自GORM documentation about associations

  

如果字段名称与变量的类型名称相同,则可以省略。