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

eager not setted at first creation #611

Open
elicas opened this issue Dec 22, 2020 · 0 comments
Open

eager not setted at first creation #611

elicas opened this issue Dec 22, 2020 · 0 comments
Labels
f: associations the associations feature in pop s: triage Some tests need to be run to confirm the issue

Comments

@elicas
Copy link

elicas commented Dec 22, 2020

Description

It would be nice to immediatly have the eager populated as soon as the object is created in order to avoid to make a find just after create to retreive the data.
For example, when we create a book for Mark it would be usefull to know that it's Mark's book and not only the book of "uuid".

Steps to Reproduce the Problem

I wrote directly in a test what it would be useful to have. The name of the test is not relevant, it's just because it's easier to explain and I think more understandable for you. (this function is in "preload_associations_test.go" line 10).

func Test_New_Implementation_For_Nplus1(t *testing.T) {
	if PDB == nil {
		t.Skip("skipping integration tests")
	}
	transaction(func(tx *Connection) {
		a := require.New(t)
		for _, name := range []string{"Mark", "Joe", "Jane"} {
			user := User{Name: nulls.NewString(name)}
			a.NoError(tx.Create(&user))

			book := Book{UserID: nulls.NewInt(user.ID)}
			a.NoError(tx.Create(&book))

			// THE COMMENTS TO READ
			// 1: a.Equal(user.ID, book.UserID.Int, "this is ok")
			// 2a: a.Equal(user.ID, book.User.ID, "but it would be cool to have it like that")
			// 2b: a.Equal(user.Name, book.User.Name, "in order to directly have the name for example")

			writer := Writer{Name: "Larry", BookID: book.ID}
			a.NoError(tx.Create(&writer))

			if name == "Mark" {
				song := Song{UserID: user.ID}
				a.NoError(tx.Create(&song))

				address := Address{Street: "Pop"}
				a.NoError(tx.Create(&address))

				home := UsersAddress{UserID: user.ID, AddressID: address.ID}
				a.NoError(tx.Create(&home))
			}
		}

		users := []User{}
		a.NoError(tx.All(&users))

		// FILL THE HAS-MANY and HAS_ONE
		a.NoError(preload(tx, &users))

		a.Len(users[0].Books, 1)
		a.Len(users[1].Books, 1)
		a.Len(users[2].Books, 1)
		a.Equal(users[0].FavoriteSong.UserID, users[0].ID)
		a.Len(users[0].Houses, 1)

		book := Book{}
		a.NoError(tx.First(&book))
		a.NoError(preload(tx, &book))
		a.Len(book.Writers, 1)
		a.Equal("Larry", book.Writers[0].Name)
		a.Equal("Mark", book.User.Name.String)
	})
}

Expected Behavior

To have the information of the book's owner, so the two lines in comments: 2a and 2b.

Actual Behavior

We only have the first line in comments: 1.

Info

I am with Linux and using v5.3.1 of Pop through v0.16.17 of Buffalo

@sio4 sio4 added s: triage Some tests need to be run to confirm the issue f: associations the associations feature in pop labels Sep 20, 2022
@sio4 sio4 added this to the v6.1.0 milestone Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: associations the associations feature in pop s: triage Some tests need to be run to confirm the issue
Projects
None yet
Development

No branches or pull requests

2 participants