We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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
The text was updated successfully, but these errors were encountered:
wikylyu sounds good. Could you send a PR with the fix?
Sorry, something went wrong.
fix: incorrect table relationship panic message uptrace#791
ad41888
Merge pull request #794 from wikylyu/master
c37cb8a
fix: incorrect table relationship panic message #791
Merged, thanks.
No branches or pull requests
Have a look at following code
When I run that code. Panic occurs like below
The problem is that panic message is incorrect.
TableB of course has column table_a_id. The right error message should be
The text was updated successfully, but these errors were encountered: