-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
Strange issue using many .Relation()
#524
Comments
This does not look like a known error to me. I will need more details to tell more. |
I'm on a family trip these days and I don't know how much free time I'll have. Can you write a small test with many structs to reproduce that number of relations? I think is all good on my side, I mean everything else (and other .Relations() too (though not that much together)) are working perfectly. |
If you cannot I will in a few days... |
I can reproduce this issue. The problem appears to be something to do with Relation being called with structs deeper than one level (In OP's case, something of The data structure I'm using is roughly as follows: type BatchRecord struct {
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()"`
UploadDate time.Time `bun:"upload_date,notnull,type:timestamp,default:current_timestamp"`
}
type Supplier struct {
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()"`
BatchID uuid.UUID `bun:"batch_id,pk,type:uuid"`
Batch *BatchRecord `bun:"rel:has-one,join:batch_id=id"`
Number int `bun:"supplier_number,notnull"`
}
type Product struct {
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()"`
BatchID uuid.UUID `bun:"batch_id,pk,type:uuid"`
Batch *BatchRecord `bun:"rel:has-one,join:batch_id=id"`
Number int `bun:"product_number,unique,notnull"`
SupplierNumber int `bun:"supplier_number,notnull"`
Supplier *Supplier `bun:",rel:has-one,join:supplier_number=supplier_number"`
}
type Price struct {
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()"`
BatchID uuid.UUID `bun:"batch_id,pk,type:uuid"`
Batch *BatchRecord `bun:"rel:has-one,join:batch_id=id"`
SupplierNumber int `bun:"supplier_number,notnull"`
Supplier *Supplier `bun:",rel:has-one,join:supplier_number=supplier_number"`
ProductNumber int `bun:"product_number,notnull"`
Product *Product `bun:",rel:has-one,join:product_number=product_number"`
Cost float32 `bun:"cost"`
Currency string `bun:"currency"`
} Select query: id := uuid.MustParse("0000-uuid-here-0000") // google/uuid type, a known value
val := &Price{}
err := NewSelect().
Model((*Price)(nil)).
Relation("Batch").
Relation("Supplier").
Relation("Supplier.Batch").
Relation("Product").
Relation("Product.Batch").
Relation("Product.Supplier").
Relation("Product.Supplier.Batch").
Where("?TableName.id = ?", id).
Scan(context.Background(), val) If anyone can advise whether this is a bug or me being a doofus, I'd appreciate it 😄 |
It seems to me that I am facing a similar problem. Simplified models:
Simplified query:
Generated sql:
The generated request is working. But bun gives an error
But in other places such relations work fine. |
I'll fork and write a test for this issue when I find the time this weekend/next week, then work back from there to see if I can fix it |
This is HARD to fix for me and I don't know what to do. Can you please help us, @vmihailenco? Sorry to bother you, really, sorry. |
Struggling to reproduce in test. Running low on time to continue testing tonight, but I have a feeling it could be something to do with using UUIDs for the primary key column. I'll give that a try next. https://github.com/tinyfluffs/bun/commit/670b175504ca2d4ff930010bf5e91303c1e4f992 |
Nope, still cannot reproduce in test, even with UUID. https://github.com/tinyfluffs/bun/blob/fix/524/internal/dbtest/orm_deep_relation_test.go |
I'm not using uuid at all. |
No worries, managed to reproduce now. Will dive deeper. |
Please post the initial reproduction here for @vmihailenco that can help you. |
Found the culprit. It appears to be down to a difference in behavior between these two calls to // Broken, because a new structTableModel{} is created and the joins are not copied across from the original model
ctx := context.Background()
val := new(Price)
err := NewSelect().
Model((*Price)(nil)).
Relation("Batch").
Relation("Supplier").
Relation("Supplier.Batch").
Relation("Product").
Relation("Product.Batch").
Relation("Product.Supplier").
Relation("Product.Supplier.Batch").
Where("?TableName.id = ?", id).
Scan(ctx, val) // This works, because the scan model is referenced in Model() instead of being sent to Scan() as a parameter
ctx := context.Background()
value := new(Price)
err := NewSelect().
Model(val).
Relation("Batch").
Relation("Supplier").
Relation("Supplier.Batch").
Relation("Product").
Relation("Product.Batch").
Relation("Product.Supplier").
Relation("Product.Supplier.Batch").
Where("?TableName.id = ?", id).
Scan(ctx) To me, this appears to be unwanted behavior? Surely scan should treat it the same as if the Model was already requested in the select query? My test branch is over here Equally, this yields an unexpected error because the model wasn't requested, but is scanned instead: val := new(Price)
db.NewSelect().
Where("?TableName.id = ?", price.ID).
Relation("Batch").
Relation("Supplier").
Relation("Supplier.Batch").
Relation("Product").
Relation("Product.Batch").
Relation("Product.Supplier").
Relation("Product.Supplier.Batch").
Limit(1).
Scan(ctx, val) |
@tinyfluffs thank you very much! |
@vmihailenco this is HUGE. Can you please help us? |
I'm facing the same issue here... I think the error is related to column length in my case. It looks like bun is shortening the column name: "sql: Scan error on column index 14, name \"operation_step_instance__operation_instance__****_transaction_u\": bun: OperationInstance does not have column \"****_transaction_u\"", The " |
@frederikhors any update on this? |
@un-versed you should ask to the maintainer. I'm leaving Bun for this issue too. |
@frederikhors oh :( bun is a great tool, but this issue is weird... @vmihailenco any ideas? |
@frederikhors btw, just gimme some info: were you using the pgx driver with bun? |
@un-versed I think this may be a separate bug, but I've also encountered the issue. Got a fix in for a related problem, but if I find time during my midsummer break next week, I'll also dig into this one. |
@tinyfluffs I created a discussion with more details after some research #566 I found the same issue in another ORM |
The original problem with different behaviour between I think the first variant should never be used at all for now and documentation updated accordingly (now it has many examples like From my understanding the problem is the following:
Possible fixes that I can think of: |
Option 3: do not use ORM at all. 😄 |
Perhaps there is some connection with the order of the fields in the original structure or the connection of two foreign keys to one table? |
This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed. |
I don't know why but if I use these Relation()` calls I get the below error:
As you can see there is the term
author
which is referring to aCommerce
struct column, but that is called:AuthorID
henceauthor_id
in SQL.Is there any limit to usable
Relation()
?Is there any limit on the length of parsable SQL?
The text was updated successfully, but these errors were encountered: