go-rel / rel

:gem: Modern ORM for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API
https://go-rel.github.io/
MIT License
744 stars 58 forks source link

Preload issue with REL 0.41 / Go 1.22 #364

Closed nitaigao closed 4 months ago

nitaigao commented 4 months ago

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:

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 commented 4 months ago

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: https://github.com/go-rel/rel/commit/ea91f66ee9217dcdc924eec58fbfd3a8bb5ac788

Fs02 commented 4 months ago

Thanks for the detailed description!

I'm able to fix this in this PR: https://github.com/go-rel/rel/pull/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 commented 4 months ago

I can confirm it's working nicely with the https://github.com/go-rel/rel/pull/365 fix

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