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

Preload issue with REL 0.41 / Go 1.22 #364

Closed
nitaigao opened this issue Feb 28, 2024 · 3 comments · Fixed by #365
Closed

Preload issue with REL 0.41 / Go 1.22 #364

nitaigao opened this issue Feb 28, 2024 · 3 comments · Fixed by #365

Comments

@nitaigao
Copy link

nitaigao commented Feb 28, 2024

Hi, firstly thanks for this awesome library.

There seems to be an issue with the preloading that has been introduced since rel 0.39.0. The issue causes the nested relations of a preload to be accidentally duplicated.

I discovered this when trying to upgrade from go v1.18 to v.22 which meant that I needed the latest REL to be compatible.

I have tried to investigate but it's not clear to me how to fix the problem.

Versions:

  • go v1.22.0
  • rel v0.41.0

Here's a quick repro:

BEGIN;

CREATE TABLE questions (
	id SERIAL PRIMARY KEY,
	text TEXT
);

CREATE TABLE answers (
	id SERIAL PRIMARY KEY,
	question_id INTEGER REFERENCES questions(id)
);

CREATE TABLE scheduled_questions (
	id SERIAL PRIMARY KEY,
	question_id INTEGER REFERENCES questions(id)
);

INSERT INTO questions (text) VALUES ('1');
INSERT INTO answers (question_id) VALUES (1);

INSERT INTO scheduled_questions (question_id) VALUES (1);
INSERT INTO scheduled_questions (question_id) VALUES (1);

COMMIT;
package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/go-rel/postgres"
	"github.com/go-rel/rel"

	_ "github.com/lib/pq"
)

type ScheduledQuestion struct {
	ID         int
	QuestionID int
	Question   *Question
}

type Question struct {
	ID      int
	Answers []Answers
}

type Answers struct {
	ID         int
	QuestionID int
}

func main() {
	adapter, err := postgres.Open("postgres://localhost/schedule_dev?sslmode=disable")
	if err != nil {
		panic(err)
	}
	db := rel.New(adapter)

	ctx := context.Background()

	scheduledQuestions := []ScheduledQuestion{}
	db.MustFindAll(ctx, &scheduledQuestions)
	db.MustPreload(ctx, &scheduledQuestions, "question")
	db.MustPreload(ctx, &scheduledQuestions, "question.answers")

	bytes, err := json.MarshalIndent(&scheduledQuestions, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bytes))
}

output:

[
  {
    "ID": 1,
    "QuestionID": 1,
    "Question": {
      "ID": 1,
      "Answers": [
        {
          "ID": 1,
          "QuestionID": 1
        },
        {
          "ID": 1, // duplicated
          "QuestionID": 1 
        }
      ]
    }
  },
  {
    "ID": 2,
    "QuestionID": 1,
    "Question": {
      "ID": 1,
      "Answers": [
        {
          "ID": 1,
          "QuestionID": 1 
        },
        {
          "ID": 1, // duplicated
          "QuestionID": 1
        }
      ]
    }
  }
]

This result should be:

[
  {
    "ID": 1,
    "QuestionID": 1,
    "Question": {
      "ID": 1,
      "Answers": [
        {
          "ID": 1,
          "QuestionID": 1
        }
      ]
    }
  },
  {
    "ID": 2,
    "QuestionID": 1,
    "Question": {
      "ID": 1,
      "Answers": [
        {
          "ID": 1,
          "QuestionID": 1
        }
      ]
    }
  }
]
@nitaigao
Copy link
Author

nitaigao commented Mar 1, 2024

Both unique question object seem to have a pointer to a single underlying answers collection. I think this is the source of the duplication.

I think it's related to a change in this commit: ea91f66

@Fs02
Copy link
Member

Fs02 commented Mar 1, 2024

Thanks for the detailed description!

I'm able to fix this in this PR: #365
can you verify if it's fixed your issue?

I think the main issue is because pointer is used when the same record is referenced multiple time
without above fix, the following should work correctly:

type ScheduledQuestion struct {
	ID         int
	QuestionID int
	Question   Question // <--- not using pointer here
}

@nitaigao
Copy link
Author

nitaigao commented Mar 4, 2024

I can confirm it's working nicely with the #365 fix

Thanks for looking into it so quickly, I really appreciate it.

@Fs02 Fs02 closed this as completed in #365 Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants