gorm Many To Many Select 出现无效的关联 [] 错误

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

gorm Many To Many Select gives invalid association [] error

问题

这是我的数据库模式:

users表:

id uuid PRIMARY KEY, title character
"1234" "ABCD"

languages表:

id uuid PRIMARY KEY, name character
"1122" "eng"
"1133" "man"

user_languages表:

user_id uuid, language_id uuid
"1234" "1122"
"1234" "1133"

这是我的代码:

type User struct {
    Id        uuid.UUID  `json:"id" gorm:"primary_key"`
    Title     string     `json:"title"`
    Languages []Language `json:"languages" gorm:"many2many:user_languages;"`
}

type Language struct {
    ID   uuid.UUID `json:"id" gorm:"primary_key"`
    Name string    `json:"name"`
}

func GetUser(id string) User {
    user := User{}
    languages := Language{}

    db.Where("id = ?", id).Find(&user) 
    // SELECT * FROM users WHERE 'id' = id;

    db.Model(&user).Related(&languages) 
    // SELECT * FROM "languages" INNER JOIN "user_languages" ON "user_languages"."language_id" = "languages"."id" WHERE "user_languages"."user_id" = 111

    return user
}

我期望得到以下结果:

{
    "id": "1234",
    "title": "ABCD",
    "languages": [{
        "id": "1122",
        "name": "eng"
    }, {
        "id": "1122",
        "name": "eng"
    }]
}

但是我在控制台上得到了invalid association []的错误提示,添加gorm日志记录器也没有提供更多信息。

即使我只能得到一个类似于语言名称数组的"languages"对象,也可以接受:

"languages": ["eng", "man"]
英文:

This is my db schema:

users :

id uuid PRIMARY KEY ,	title character
"1234"					"ABCD"

languages :

id uuid PRIMARY KEY ,	name character
"1122"					"eng"
"1133"					"man"

user_languages :

user_id uuid, 	language_id uuid
"1234"			"1122"
"1234"			"1133"

This is my code:

type User struct {
   Id           uuid.UUID  `json:"id" gorm:"primary_key"`
   Title        string     `json:"title"`
   Languages    []Language `json:"languages" gorm:"many2many:user_languages;"`
}

type Language struct {
   ID   uuid.UUID `json:"id" gorm:"primary_key"`
   Name string    `json:"name"`
}

func GetUser(id string) User {
   user := User{}
   languages := Language{}

   db.Where("id = ?", id).Find(&user) 
   // SELECT * FROM users WHERE 'id' = id;

   db.Model(&user).Related(&languages) 
   // SELECT * FROM "languages" INNER JOIN "user_languages" ON "user_languages"."language_id" = "languages"."id" WHERE "user_languages"."user_id" = 111

   return user
}

I am expecting this result:

{
   "id": "1234",
   "title": "ABCD",
   "languages" : [{ 
               "id" : "1122",
               "name" : "eng"
           },{ 
               "id" : "1122",
               "name" : "eng"
           }]
}

But I am getting invalid association [] on console, adding a gorm logger did not provide more information.

Even if I can only get a "languages" object like an array of languages names, this is also fine:

"languages" : ["eng", "man"]

答案1

得分: 10

你可以尝试使用以下代码:

db.Model(&user).Related(&languages, "Languages") 

更多示例可以在test中找到。

英文:

You may try:

db.Model(&user).Related(&languages, "Languages") 

More examples can be find in the test

答案2

得分: 1

我认为你需要使用Association。你还可以使用db.First(&user, id)更简洁地获取匹配的用户。

func GetUser(id string) User {
    var user User

    db.First(&user, id)

    db.Model(&user).Association("Languages").Find(&user.Languages) 

    return user
}

请注意,我已经将代码中的&替换为了正确的符号。

英文:

I think you need to use Association. You can also get the matching user more succinctly using db.First(&user, id).

func GetUser(id string) User {
    var user User

    db.First(&user, id)

    db.Model(&user).Association("Languages").Find(&user.Languages) 

    return user
}

huangapple
  • 本文由 发表于 2016年1月8日 07:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/34667199.html
匿名

发表评论

匿名网友

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

确定