Your Question
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string
CompanyID string
Company Company `gorm:"references:CompanyID"`
}
type Company struct {
CompanyID int
Code string
Name string
}
func main() {
dsn := "root:123456@tcp(localhost:3306)/learn-gorm?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&User{}, &Company{})
}
Error 1215: Cannot add foreign key constraint
[14.528ms] [rows:0] CREATE TABLE `users` (`id` bigint unsigned AUTO_INCREMENT,`created_at` datetime(3) NULL,`updated_at` datetime(3) NULL,`deleted_at` datetime(3) NULL,`name` longtext,`company_id` bigint,PRIMARY KEY (`id`),INDEX `idx_users_deleted_at` (`deleted_at`),CONSTRAINT `fk_users_company` FOREIGN KEY (`company_id`) REFERENCES `companies`(`company_id`))
mysql --version
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.19, for macos10.12 (x86_64) using EditLine wrapper
go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/chenhailong/Library/Caches/go-build"
GOENV="/Users/chenhailong/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/chenhailong/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/chenhailong/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qk/bj_mbvq54zs663fjmkykq4cc0000gn/T/go-build3202474337=/tmp/go-build -gno-record-gcc-switches -fno-common"
The document you expected this should be explained
I wrote the code according to this document https://gorm.io/docs/belongs_to.html#Override-References
Expected answer
i want how to use references
Comment From: OopsLouie
did u solve this? same thing occurred to me Error 1215 (HY000): Cannot add foreign key constraint
Comment From: OopsLouie
code
type Device struct {
ID
DeviceSN
DeviceName
...
}
type Alarm struct {
ID
DeviceSN
...
Device Device `gorm:"foreignKey:DeviceSN;references:DeviceSN"
}
failed with Error 1215 (HY000): Cannot add foreign key constraint
code test success using gorm@v1.20.7 failed using gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11
when success the create sql is
xxxxxxxxxx, CONSTRAINT `fk_alarms_device` FOREIGN KEY (`device_sn`) REFERENCES `devices`(`device_sn`))
when fail the create sql is
xxxxxxxxxx, CONSTRAINT `fk_alarms_device` FOREIGN KEY (`device_sn`) REFERENCES `alarms`(`device_sn`))
is this a new bug?
Comment From: OopsLouie
never mind, I finally find a solution to make it right at v1.24.7 or later use code bellow
Device Device `gorm:"foreignKey:DeviceSN;references:DeviceSN;belongsTo"
anyway, I think this belongsTo tag should be mentioned in DOC
Comment From: a631807682
When the associated model has the same field name, the specified (otherwise it will be determined by guessing) association relationship. Welcome to craete a PR to help us improve the document.