Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Not compatible with GORM database foreign keys #962

Open
@procodr

Description

@procodr

Output ofgarble version:

mvdan.cc/garble v0.14.3-0.20250615171651-59eee83beb8b

Build settings:
-buildmode exe
-compiler gc
CGO_ENABLED 1
GOARCH amd64
GOOS windows
GOAMD64 v1

Output ofgo env in your module/workspace:

set AR=arset CC=gccset CGO_CFLAGS=-O2 -gset CGO_CPPFLAGS=set CGO_CXXFLAGS=-O2 -gset CGO_ENABLED=1set CGO_FFLAGS=-O2 -gset CGO_LDFLAGS=-O2 -gset CXX=g++set GCCGO=gccgoset GO111MODULE=set GOAMD64=v1set GOARCH=amd64set GOAUTH=netrcset GOBIN=set GOCACHE=C:\Users\User\AppData\Local\go-buildset GOCACHEPROG=set GODEBUG=set GOENV=C:\Users\User\AppData\Roaming\go\envset GOEXE=.exeset GOEXPERIMENT=set GOFIPS140=offset GOFLAGS=set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\User\AppData\Local\Temp\go-build3584084421=/tmp/go-build -gno-record-gcc-switchesset GOHOSTARCH=amd64set GOHOSTOS=windowsset GOINSECURE=set GOMOD=C:\SRC\PR\GarbleDbTest\go.modset GOMODCACHE=C:\Users\User\go\pkg\modset GONOPROXY=set GONOSUMDB=set GOOS=windowsset GOPATH=C:\Users\User\goset GOPRIVATE=set GOPROXY=https://proxy.golang.org,directset GOROOT=C:\Program Files\Goset GOSUMDB=sum.golang.orgset GOTELEMETRY=localset GOTELEMETRYDIR=C:\Users\User\AppData\Roaming\go\telemetryset GOTMPDIR=set GOTOOLCHAIN=autoset GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64set GOVCS=set GOVERSION=go1.24.4set GOWORK=set PKG_CONFIG=pkg-config

What did you do?

I wrote a simple app which creates a database with foreign keys using GORM:

package mainimport ("gorm.io/driver/sqlite""gorm.io/gorm")typePersonstruct {FirstNamestringLastNamestringIDuint`gorm:"primarykey"`AccountIDuint}typeAccountstruct {UsernamestringPasswordstringPersonPersonIDuint`gorm:"primarykey"`}funcinitDb()*gorm.DB {db,err:=gorm.Open(sqlite.Open("db"),&gorm.Config{})iferr!=nil {panic("failed to connect database, err: "+err.Error())}err=db.AutoMigrate(&Person{},&Account{})iferr!=nil {panic("failed to migrate database, err: "+err.Error())}returndb}funcmain() {initDb()println("DB initialization done without errors")}

Here,Person table has a foreign key fromAccount'sID field.

Image

then usedgarble run . to test.

What did you see happen?

It gives database migration errors:

2025/07/13 16:25:25 jC3ArRL2L.go:1[error] invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 at8GqhMZxTD.go:1[error] failed to parse value &main.H77fYIyZ{GVoxSE4:"", Eotrl4:"", Klw32sEWYaRy:main.Sdb46x4h{TXa5Yw:"", VdWiPQ:"", V6bOdBlDOUF:0x0, UOmehj3zao:0x0}, NrrXXYz9:0x0}, got error invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 jC3ArRL2L.go:1[error] invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 jC3ArRL2L.go:1[error] invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 at8GqhMZxTD.go:1[error] failed to parse value &main.H77fYIyZ{GVoxSE4:"", Eotrl4:"", Klw32sEWYaRy:main.Sdb46x4h{TXa5Yw:"", VdWiPQ:"", V6bOdBlDOUF:0x0, UOmehj3zao:0x0}, NrrXXYz9:0x0}, got error invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 jC3ArRL2L.go:1[error] invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interfacepanic: failed to migrate database, err: invalid field found for struct main.H77fYIyZ's field Klw32sEWYaRy: define a valid foreign key for relations or implement the Valuer/Scanner interfacegoroutine 1 [running]:main.vyqfilwhu()        qPa_DOYbBl.go:1 +0x186main.main()        tp5dyEJH.go:1 +0x13exit status 2exit status 1

Reversed:

2025/07/13 16:25:25 gorm.io/gorm/schema/schema.go:334[error] invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 gorm.io/gorm/migrator/migrator.go:922[error] failed to parse value &main.Account{Username:"", Password:"", Person:main.Person{FirstName:"", LastName:"", ID:0x0, AccountID:0x0}, ID:0x0}, got error invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 gorm.io/gorm/schema/schema.go:334[error] invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 gorm.io/gorm/schema/schema.go:334[error] invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 gorm.io/gorm/migrator/migrator.go:922[error] failed to parse value &main.Account{Username:"", Password:"", Person:main.Person{FirstName:"", LastName:"", ID:0x0, AccountID:0x0}, ID:0x0}, got error invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface2025/07/13 16:25:25 gorm.io/gorm/schema/schema.go:334[error] invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interfacepanic: failed to migrate database, err: invalid field found for struct main.Account's field Person: define a valid foreign key for relations or implement the Valuer/Scanner interface

It seems GORM is unable to find theAccountID field inPerson struct.

What did you expect to see?

running this app must give this message which says the database is created without any errors:

DB initialization done without errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp