自引用结构,带有预加载父母的记录在父母和孩子之间切换

I have a struct which is self referencing, and when I try to fetch the data by using Preload the record is somehow switched between the parent and the child.

In this example I created these records:

  1. A Person called Manager 1
  2. A Person called Employee 1 which has Manager 1 as his parent
  3. A Person called Manager 2
  4. A Person called Employee 2 which has Manager 2 as his parent

Here is the code:

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

type Person struct {
    ID      int `gorm:"primary_key"`
    Name    string
    Parent  int
    Parent_ *Person `gorm:"foreignkey:parent"`
}

func (Person) TableName() string {
    return "go_person"
}

const (
    DB_ADAPTER  = "postgres"
    DB_HOST     = "localhost"
    DB_USER     = "postgres"
    DB_PASSWORD = "test"
    DB_NAME     = "test"
    DB_SSLMODE  = "disable"
)

func main() {
    db, err := gorm.Open(DB_ADAPTER, "host="+DB_HOST+" user="+DB_USER+" dbname="+DB_NAME+" sslmode=disable"+" password="+DB_PASSWORD+"")
    if err != nil {
        panic("failed to open storage")
    }
    defer db.Close()
    db.AutoMigrate(&Person{})

    db.Delete(Person{})

    newManager := Person{
        Name    : "Manager 1",
    }
    db.Create(&newManager)

    managerID := newManager.ID

    newEmployee := Person{
        Name    : "Employee 1",
        Parent  : managerID,
    }
    db.Create(&newEmployee)

    newManager = Person{
        Name    : "Manager 2",
    }
    db.Create(&newManager)

    managerID = newManager.ID

    newEmployee = Person{
        Name    : "Employee 2",
        Parent  : managerID,
    }
    db.Create(&newEmployee)

    persons := []Person{}

    db.Preload("Parent_").Find(&persons)

    for _, person := range persons {
        fmt.Println("Name    :",person.Name)
        if person.Parent_ != nil {
            fmt.Println("Manager :",person.Parent_.Name)
        }
        fmt.Println("---------------------------------")
    }
}

In this example I expect to have this as the output:

Name    : Manager 1
---------------------------------
Name    : Employee 1
Manager : Manager 1
---------------------------------
Name    : Manager 2
---------------------------------
Name    : Employee 2
Manager : Manager 2
---------------------------------

But instead I got this:

Name    : Manager 1
Manager : Employee 1
---------------------------------
Name    : Employee 1
---------------------------------
Name    : Manager 2
Manager : Employee 2
---------------------------------
Name    : Employee 2
---------------------------------

Why?