GithubHelp home page GithubHelp logo

Comments (8)

a631807682 avatar a631807682 commented on August 17, 2024 1

Preload is a combined method, you need to specify their relationship conditions separately, in this case you can use Joins.

from gorm.io.

a631807682 avatar a631807682 commented on August 17, 2024

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.

droslean avatar droslean commented on August 17, 2024

@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.

a631807682 avatar a631807682 commented on August 17, 2024

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.

droslean avatar droslean commented on August 17, 2024

@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.

a631807682 avatar a631807682 commented on August 17, 2024

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.

droslean avatar droslean commented on August 17, 2024

@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.

droslean avatar droslean commented on August 17, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.