Gorm: Misleading Has Many Association

Created on 27 Oct 2018  Â·  6Comments  Â·  Source: go-gorm/gorm

Your issue may already be reported! Please search on the issue track before creating one.

What version of Go are you using (go version)?

➜  awesomeProject go version    
go version go1.11.1 darwin/amd64

Which database and its version are you using?

mysql> select version();
+-------------------------+
| version()               |
+-------------------------+
| 5.7.20-0ubuntu0.16.04.1 |
+-------------------------+

Please provide a complete runnable program to reproduce your issue. IMPORTANT

This code cannot work which I follow the doc (http://gorm.io/docs/has_many.html#Foreign-Key):

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

type User struct {
    gorm.Model
    CreditCards []CreditCard `gorm:"foreignkey:UserRefer"`
}

type CreditCard struct {
    gorm.Model
    Number    string
    UserRefer uint
}

func main() {
    url := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local",
        "g10guang", "workhard", "139.199.163.36", "3306", "goweb")
    db, err := gorm.Open("mysql", url)
    if err != nil || db == nil {
        fmt.Println(err)
    }
    db.LogMode(true)
    db.SingularTable(true)

    db.AutoMigrate(&User{})
    db.AutoMigrate(&CreditCard{})

    user := User{}
    db.First(&user)

    fmt.Println(user)

    var cs []CreditCard

    db.Model(&user).Related(&cs)

    fmt.Println(cs)
}

output:

(/Users/g10guang/go/src/github.com/g10guang/awesomeProject/main.go:34) 
[2018-10-27 17:49:07]  [7.81ms]  SELECT * FROM `user`  WHERE `user`.`deleted_at` IS NULL ORDER BY `user`.`id` ASC LIMIT 1  
[1 rows affected or returned ] 
{{1 2018-10-27 17:25:20 +0800 CST 2018-10-27 17:25:20 +0800 CST <nil>} []}

(/Users/g10guang/go/src/github.com/g10guang/awesomeProject/main.go:40) 
[2018-10-27 17:49:07]  invalid association [] 
[]

I must set the attr name:

db.Model(&user).Related(&cs, "CreditCards")

It works.

Or the CreditCard.UserRefer replace with CreditCard.UserID it Work too.

So I think the behavior is so misleading. It should be noticed in the doc.

gorm_v1 document

All 6 comments

+1 for this issue,
i think it is an very important bug

+1
I lost a lot of time because of this failure

+1

+1

that really strange,even the gorm tag is setting but the related method don't even use it..
Can't understating such behavior ,those "foreignkey" tag only available for preloading,but not for relate,and we need to rewrite those foreignkey just everywhere,then what is the meaning of foreignkey tag.I consider this as some kind of bug....I don't really think it only the document issue anyway....

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

littletwolee picture littletwolee  Â·  3Comments

leebrooks0 picture leebrooks0  Â·  3Comments

bramp picture bramp  Â·  3Comments

kumarsiva07 picture kumarsiva07  Â·  3Comments

izouxv picture izouxv  Â·  3Comments