Reproduction given in #519 AND #520(simpler)

Description

This Bug occurs in go v1.23.10 with postgres v1.3.10. The Bug does not appear in Gorm v1.23.4 with postgres v1.3.5

We got an User struct with ID, a Revision, a name and one Embedded Struct, which is Referenced by a pointer

type User struct {
    ID       string
    Revision int `gorm:"primaryKey"`
    Name     string
    Embed    *Embed `gorm:"embedded;embeddedPrefix:embed_"`
}
type Embed struct {
    FieldName string
}

The goal is to get the newest Object (with the highest revision) for given userIDs In SQL (PostgreSQL) this should look like this:

SELECT "users"."id", "users"."revision", "users"."name", "users"."embed_field_name"
FROM "users"
         RIGHT JOIN (SELECT MAX(revision) AS latest, id
                     FROM "users"
                     WHERE users.id IN <userIDs>
                     GROUP BY "id") AS r ON r.latest = users.revision

Gorm generates exactly that SQL Statement. So far, so good.

In the Testcase we Create 3 Users in our Database:

    testuser := []User{
        {
            ID:       "ID1",
            Revision: 1,
            Name:     "otherName",
            Embed: &Embed{
                FieldName: "Field",
            },
        },
        {
            ID:       "ID1",
            Revision: 2,
            Name:     "jinzhu",
            Embed: &Embed{
                FieldName: "Field",
            },
        },
        {
            ID:       "ID2",
            Revision: 3,
        },
    }

I expect to get the following Response after asking for the newest Users with IDs :

{
    ID:       "ID1",
    Revision: 2,
    Name:     "jinzhu",
    Embed: &Embed{
        FieldName: "Field",
    },
},
{
    ID:       "ID2",
    Revision: 3,
}

The problem is that Gorm actually returns following:

        {
            ID:       "ID1",
            Revision: 2,
            Name:     "jinzhu",
            Embed: // Reference to 0xc00023cb30
        },
        {
            ID:       "ID2",
            Revision: 3,
            Name:     "",
            Embed: // Reference to 0xc00023cb30
        },

Both Embed points to the same Object, even if the SQL-Statement returns Embed of ID2 as nil

Comment From: J-Rocke

@jinzhu I created another more simple reproducer here: https://github.com/go-gorm/playground/pull/520

Comment From: a631807682

Upgrade gorm version, duplicated of https://github.com/go-gorm/gorm/issues/5575

Comment From: J-Rocke

The upgrade fixed the problem. Thank you.