volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Generated tests fail with type definition replacement #487

Closed efevans closed 5 years ago

efevans commented 5 years ago

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v3.0.1

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE public."master_operation"
(
    id serial NOT NULL,
    master_type integer NOT NULL,
    PRIMARY KEY (id)
)

Further information. What did you do, what did you expect?

We are running into an issue when running golang tests with the generated models. The command for building is sqlboiler.exe psql --wipe --no-hooks -o .\models\, and for running the tests is go test .\models\. We have a type definition type MasterType int, and in sqlboiler.toml a replacement configuration specified for the master_type interger column to use MasterType type definition:

[[types]]
    [types.match]
    name = "master_type"

    [types.replace]
    type    =   "types.MasterType"

    [types.imports]
    third_party = ['"github.com/myuser/SqlBoilerTest/types"']

in /github.com/myuser/SqlBoilerTest/types/master.go:

package types

type MasterType int

The error shows the following:

PS C:\MyCode\src\github.com\myuser\SqlBoilerTest> go test .\models\
panic: reflect.Set: value of type int is not assignable to type types.MasterType [recovered]
        panic: reflect.Set: value of type int is not assignable to type types.MasterType

goroutine 43 [running]:
testing.tRunner.func1(0xc0423043c0)
        C:/Go/src/testing/testing.go:742 +0x2a4
panic(0x8a7b20, 0xc042218360)
        C:/Go/src/runtime/panic.go:502 +0x237
reflect.Value.assignTo(0x8a71e0, 0xc04229dd30, 0x82, 0x958fb4, 0xb, 0x8a6660, 0x0, 0x8910a0, 0x8a71e0, 0xc04229dd30)
        C:/Go/src/reflect/value.go:2235 +0x42e
reflect.Value.Set(0x8a6660, 0xc04221a3c8, 0x182, 0x8a71e0, 0xc04229dd30, 0x82)
        C:/Go/src/reflect/value.go:1373 +0xab
github.com/volatiletech/sqlboiler/randomize.randomizeField(0xc04229dd10, 0x8a6660, 0xc04221a3c8, 0x182, 0x955fe8, 0x7, 0x9c9a01, 0x8a6660, 0x8d0d5a)
        C:/MyCode/src/github.com/volatiletech/sqlboiler/randomize/randomize.go:162 +0x21e
github.com/volatiletech/sqlboiler/randomize.Struct(0xc04229dd10, 0x8f6ee0, 0xc04221a3c0, 0xc0420f75c0, 0x1, 0xbf4050, 0x1, 0x1, 0x508c98, 0xc042306240)
        C:/MyCode/src/github.com/volatiletech/sqlboiler/randomize/randomize.go:105 +0x43a
github.com/myuser/SqlBoilerTest/models.testMasterOperationsDelete(0xc0423043c0)
        C:/MyCode/src/github.com/myuser/SqlBoilerTest/models/master_operation_test.go:40 +0xcf
testing.tRunner(0xc0423043c0, 0x979498)
        C:/Go/src/testing/testing.go:777 +0xd7
created by testing.(*T).Run
        C:/Go/src/testing/testing.go:824 +0x2e7
FAIL    github.com/myuser/SqlBoilerTest/models 2.498s

Also to note that using this kind of replacement runs fine during normal operation, only when running the tests does the error occur.

aarondl commented 5 years ago

I notice you're on an older version of sqlboiler. There have been a few releases since then and importantly several places that were not using aliases have been updated to be alias-aware. Can you try again on the new version?

efevans commented 5 years ago

Still seeing the issue in SqlBoiler v3.2.0. Updated the sqlboiler with go get -u -t github.com/volatiletech/sqlboiler, rebuilt the models with sqlboiler.exe psql --wipe --no-hooks, then ran the tests with go test .\models\.

panic: reflect.Set: value of type int is not assignable to type types.MasterType [recovered]
        panic: reflect.Set: value of type int is not assignable to type types.MasterType

goroutine 27 [running]:
testing.tRunner.func1(0xc0420ecd20)
        C:/Go/src/testing/testing.go:742 +0x2a4
panic(0x8ada80, 0xc0421c4600)
        C:/Go/src/runtime/panic.go:502 +0x237
reflect.Value.assignTo(0x8ad140, 0xc0422b18f8, 0x82, 0x95fea3, 0xb, 0x8ac580, 0x0, 0x896fc0, 0x8ad140, 0xc0422b18f8)
        C:/Go/src/reflect/value.go:2235 +0x42e
reflect.Value.Set(0x8ac580, 0xc042155968, 0x182, 0x8ad140, 0xc0422b18f8, 0x82)
        C:/Go/src/reflect/value.go:1373 +0xab
github.com/volatiletech/sqlboiler/randomize.randomizeField(0xc0422b18a8, 0x8ac580, 0xc042155968, 0x182, 0x95cebb, 0x7, 0x9d0f01, 0x8ac580, 0x8d71fa)
        C:/MyCode/src/github.com/volatiletech/sqlboiler/randomize/randomize.go:162 +0x21e
github.com/volatiletech/sqlboiler/randomize.Struct(0xc0422b18a8, 0x8fdb60, 0xc042155960, 0xc04211f5c0, 0x1, 0xbfe0d0, 0x1, 0x1, 0x508d28, 0xc0420e8a20)
        C:/MyCode/src/github.com/volatiletech/sqlboiler/randomize/randomize.go:105 +0x43a
github.com/efevans/SqlBoilerTest/models.testMasterOperationsDelete(0xc0420ecd20)
        C:/MyCode/src/github.com/efevans/SqlBoilerTest/models/master_operation_test.go:40 +0xcf
testing.tRunner(0xc0420ecd20, 0x980558)
        C:/Go/src/testing/testing.go:777 +0xd7
created by testing.(*T).Run
        C:/Go/src/testing/testing.go:824 +0x2e7
FAIL    github.com/efevans/SqlBoilerTest/models 5.739s
aarondl commented 5 years ago

@efevans Fixed on dev, should work for all aliased types like that. Thanks for the bug report.