Comments (8)
Preload is a combined method, you need to specify their relationship conditions separately, in this case you can use Joins.
from gorm.io.
This is supported, if you still have problem, you can submit an issue and a reproduction link in gorm.
https://gorm.io/docs/preload.html#Preload-with-conditions
from gorm.io.
@a631807682 Thanks for the response. You can see the description of the issue to reproduce it. I am doing exactly what the documentation says, but still gormdb load all the contents. I have see in the debug logs that it does the correct query, but still are Contents
that shouldn't be based on the condition I added in Metadata.
from gorm.io.
I don't understand what you want to query from your description. Your Preload
restricts the query of Metadata
, but it seems that what you want to filter is Content, but you also said that SQL query is correct.
I think the scenario you describe is similar to this test case, can you tell me where is the difference
https://github.com/go-gorm/gorm/blob/master/tests/preload_test.go#L188
from gorm.io.
@a631807682 My goal is to get all Content
with empty Metadata.component
. How can I achieve this? Based on the documents, it should just work with using the Preload, but it doesn't, and I am trying to figure out why.
from gorm.io.
It cannot be reproduced, maybe you should update the gorm version, if there is still a problem, please provide a reproduction link.
func TestGORM(t *testing.T) {
DB.Migrator().DropTable(&Content{}, &Metadata{})
DB.AutoMigrate(&Content{}, &Metadata{})
contents := []Content{
{Data: "data 1", Metadata: Metadata{Component: "not empty", Locale: "en"}},
{Data: "data 2", Metadata: Metadata{Component: "", Locale: "zh"}},
{Data: "data 3", Metadata: Metadata{Component: "not empty", Locale: "en"}},
}
DB.Create(&contents)
var contents2 []Content
DB.Preload("Metadata", "component <> ''").Find(&contents2)
for i := 0; i < len(contents); i++ {
expect := contents[i]
got := contents2[i]
tests.AssertObjEqual(t, expect, got, "Data", "Domain")
if got.Data == "data 2" { // filter
tests.AssertEqual(t, got.Metadata.ID, 0)
tests.AssertEqual(t, got.Metadata.Component, "")
tests.AssertEqual(t, got.Metadata.Locale, "")
continue
}
tests.AssertObjEqual(t, expect.Metadata, got.Metadata, "Component", "Locale")
}
}
from gorm.io.
@a631807682 The problem arises when I am trying to filter the domain
on Contents
. Instead, gormdb returns all the content for the specific domain with empty Metadata.
It seems that
r.db.WithContext(ctx).Preload("Metadata", "component <> ''").Where("domain = ?", domain).Find(&contents)
The Preload
and the Where
are not connected.
from gorm.io.
@a631807682 So it seems that I should do something like:
r.db.WithContext(ctx).Joins("Metadata").Where("domain = ? AND component <> ''", domain).Find(&contents)
from gorm.io.
Related Issues (20)
- DB.Set and .Get should accept `any` as key for context.
- orm.AutoMigrate(&aModel{}) occurs error "pq: got 1 parameters but the statement requires 0" HOT 1
- [Question] AutoMigrate runs migration ever it's no field change in struct
- How can i use proload with scopes to query
- Log duplication in gen/generator.go
- [Question] Query logging/debug produces sql output inconsistent with the server executed query
- [Question] Code blocks always cover the sidebar
- [Korean Translation] I'd like to start translating it in Korean HOT 1
- [New Translation] Language Name
- [New Translation] Language Name
- [Question] Unclear when default transaction is committed
- Fix the z-index of the Contents section of the docs
- [Question] Generated SQL not correct for Update Selected Fields
- [New Translation] Traditional Chinese
- [Question]
- [New Translation] Korean
- [Question]
- [New Translation] Language Name
- [Question] Bad examples about nested struct HOT 1
- [Question] How i roll back the database??? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gorm.io.