Gorm: belongs to problem

Created on 18 Oct 2019  ·  8Comments  ·  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)?

v1.12

Which database and its version are you using?

master

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

i user demo on(https://gorm.io/docs/belongs_to.html#Foreign-Key) user "Foreign Key" it not work
code write.

Need to runnable with GORM's docker compose config or please provides your config.

package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mssql"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    _ "github.com/jinzhu/gorm/dialects/postgres"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
)

var db *gorm.DB

func init() {
    var err error
     db, err = gorm.Open("mysql", "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True")
    if err != nil {
        panic(err)
    }
    db.LogMode(true)
}

type User struct {
    gorm.Model
    Name string
}

type Profile struct {
    gorm.Model
    Name      string
    User      User `gorm:"foreignkey:UserRefer"` // 使用 UserRefer 作为外键
    UserRefer uint
}

func main() {
    var p Profile
    orm.Find(&p)
    orm.Model(&p).Related(&p.User) // but i use: orm.Model(&p).Related(&p.User,"UserRefer") it work int 
    fmt.Println(p)
}

and get the err:
invalid association []

gorm_v1

Most helpful comment

and i try has one "Association ForeignKey" . the same problem

https://gorm.io/docs/has_one.html#Association-ForeignKey

All 8 comments

and "Association ForeignKey" has the same problem on (https://gorm.io/docs/belongs_to.html#Association-ForeignKey)

and i try has one "Association ForeignKey" . the same problem

https://gorm.io/docs/has_one.html#Association-ForeignKey

Did you ever figure this out? I can't get any form of ForeignKey working. Tried every possible relationship type with very basic, dumbed down examples.

Not yet,can use the default way

me too

https://github.com/xxjwxc/gormt
i'm woking to this tools . so need to using

如果不是看到这个issue,我想我今晚都睡不着,一直在想为什么这个简单的官方例子都我走不通,而且在官方文档,百度,google都没有提出问题,真不可以思议,原来还真是个bug

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

Ganitzsh picture Ganitzsh  ·  3Comments

zeropool picture zeropool  ·  3Comments

hypertornado picture hypertornado  ·  3Comments

izouxv picture izouxv  ·  3Comments

koalacxr picture koalacxr  ·  3Comments