Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect panic message #791

Closed
wikylyu opened this issue Mar 3, 2023 · 2 comments
Closed

Incorrect panic message #791

wikylyu opened this issue Mar 3, 2023 · 2 comments

Comments

@wikylyu
Copy link
Contributor

wikylyu commented Mar 3, 2023

Have a look at following code

package main

import (
	"context"
	"database/sql"

	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect/pgdialect"
	"github.com/uptrace/bun/driver/pgdriver"
)

type TableA struct {
	bun.BaseModel `bun:"table:table_a"`
	UserID        int64 `bun:"user_id,pk,autoincrement" json:"user_id"`
}

type TableB struct {
	bun.BaseModel `bun:"table:table_b"`
	ID            int64 `bun:"id,pk,autoincrement" json:"id"`

	TableAID int64 `bun:"table_a_id,notnull" json:"table_a_id"`

	A *TableA `bun:"rel:belongs-to,join:table_a_id=id"`   // there's an error in this line. TableA's primary key is user_id, not id.
}

func main() {

	dsn := "postgres://postgres:@localhost:5432/test?sslmode=disable"
	// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
	sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))

	db := bun.NewDB(sqldb, pgdialect.New())

	b := TableB{}
	if err := db.NewSelect().Model(&b).Relation(`A`).Limit(1).Scan(context.Background()); err != nil {
		return
	}

}

When I run that code. Panic occurs like below

panic: bun: TableB belongs-to A: TableB must have column table_a_id

goroutine 1 [running]:
github.com/uptrace/bun/schema.(*Table).belongsToRelation(0xc00017e160, 0xc00017eb00)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/table.go:535 +0xaf7
github.com/uptrace/bun/schema.(*Table).initRelation(0x68b720?, 0xc00017eb00, {0x674105?, 0x3?})
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/table.go:452 +0xb2
github.com/uptrace/bun/schema.(*Table).tryRelation(0xc00017e160, 0xc00017eb00)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/table.go:431 +0x79
github.com/uptrace/bun/schema.(*Table).initRelations(0xc00017e160)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/table.go:416 +0x6f
github.com/uptrace/bun/schema.(*Table).init2(0x46c1c6?)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/table.go:130 +0x19
github.com/uptrace/bun/schema.(*tableInProgress).init2.func1()
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/tables.go:34 +0x29
sync.(*Once).doSlow(0xc00009eb60?, 0xc00009ebe8?)
	/usr/lib/go-1.19/src/sync/once.go:74 +0xc2
sync.(*Once).Do(...)
	/usr/lib/go-1.19/src/sync/once.go:65
github.com/uptrace/bun/schema.(*tableInProgress).init2(0xc0000ac338?)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/tables.go:33 +0x5a
github.com/uptrace/bun/schema.(*Tables).table(0xc0000ac300, {0x7472c0?, 0x6acd80?}, 0x0)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/tables.go:104 +0x27f
github.com/uptrace/bun/schema.(*Tables).Get(...)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/schema/tables.go:62
github.com/uptrace/bun.(*DB).Table(0x1?, {0x7472c0, 0x6acd80})
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/db.go:195 +0x45
github.com/uptrace/bun.newStructTableModelValue(0xc0000ac360, {0x678540?, 0xc0000a8ae0}, {0x6acd80?, 0xc0000a8ae0?, 0x422aa7?})
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/model_table_struct.go:46 +0x7e
github.com/uptrace/bun._newModel(0xc0000ac360, {0x678540?, 0xc0000a8ae0}, 0x0)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/model.go:123 +0x7b4
github.com/uptrace/bun.newSingleModel(...)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/model.go:75
github.com/uptrace/bun.(*baseQuery).setModel(0xc000180000, {0x678540?, 0xc0000a8ae0?})
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/query_base.go:163 +0x34
github.com/uptrace/bun.(*SelectQuery).Model(...)
	/home/wiky/Downloads/temp/vendor/github.com/uptrace/bun/query_select.go:60
main.main()
	/home/wiky/Downloads/temp/a.go:35 +0x16b
exit status 2

The problem is that panic message is incorrect.

panic: bun: TableB belongs-to A: TableB must have column table_a_id

TableB of course has column table_a_id. The right error message should be

bun: TableB belongs-to A:  TableA must have column id
@vmihailenco
Copy link
Member

wikylyu sounds good. Could you send a PR with the fix?

wikylyu added a commit to wikylyu/bun that referenced this issue Mar 4, 2023
vmihailenco added a commit that referenced this issue Mar 4, 2023
fix: incorrect table relationship panic message #791
@vmihailenco
Copy link
Member

Merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants