Auto Preloading with gorm not working as expected

时间秒杀一切 提交于 2019-12-11 12:47:38

问题


I am trying to auto-preload my models but am having difficulty doing so.

These are the models i am using :

package domain

type User struct {
    gorm.Model
    Name string
    Car Car `gorm:"auto_preload"`
    Account Account `gorm:"auto_preload"`
}

type Car struct {
    gorm.Model
    Type int
    UserID uint
}

type Account struct {
    gorm.Model
    Powerlevel int
    UserID uint
}

This is the code i am executing to test the auto-preloading feature:

func main() {

    db, err := gorm.Open("sqlite3", "./data.db")

    if err != nil {
        println(err.Error())
    }

    //Migrate the tables
    db.AutoMigrate(&domain.User{})
    db.AutoMigrate(&domain.Car{})
    db.AutoMigrate(&domain.Account{})

    // Initialize our models
    var testUser domain.User
    var car = domain.Car{Type: 1994}
    var account = domain.Account{Powerlevel: 9001}

    testUser.Name = "Jim Bob"
    testUser.Car = car
    testUser.Account = account

    // Save the user
    db.Create(&testUser)

    // Get a new user
    var newUser domain.User
    db.First(&newUser)

    println(newUser.Name)
    println(newUser.Car.Type)
    println(newUser.Account.Powerlevel)

    // Explicitly preload

    db.Preload("Car").First(&newUser)
    db.Preload("Account").First(&newUser)


    println(newUser.Name)
    println(newUser.Car.Type)
    println(newUser.Account.Powerlevel)
}

The output of the two prints are :

Jim Bob
0
0
Jim Bob
1994
9001

Why does getting the model over db.First(...) not automatically preload when i can preload them manually perfectly fine ?


回答1:


Firstly I would recommend reading the docs.
A bit of explanation here. It appears that you are using an old version more than likely. The updated docs explain to use this tag gorm:"PRELOAD", which by default is set to true. So if you would like to disable a field use gorm:"PRELOAD:false".

Afterward, use must use db.Set("gorm:auto_preload", true) for it to fetch properly. If you are going to use the preload fetch through the application then you can reassign db as such to have the auto_preload feature:

db = db.Set("gorm:auto_preload", true)

Example:

package main

import "github.com/jinzhu/gorm"
import _ "github.com/jinzhu/gorm/dialects/sqlite"

type User struct {
        gorm.Model
        Name    string
        Car     Car
        Account Account `gorm:"PRELOAD:false"`
}

type Car struct {
        gorm.Model
        Type   int
        UserID uint
}

type Account struct {
        gorm.Model
        Powerlevel int
        UserID     uint
}

func main() {
        db, err := gorm.Open("sqlite3", "./data.db")
        db.LogMode(true)
        if err != nil {
                println(err.Error())
        }
        //Migrate the tables
        db.AutoMigrate(&User{})
        db.AutoMigrate(&Car{})
        db.AutoMigrate(&Account{})

        // Initialize our models
        var testUser User
        var car = Car{Type: 1994}
        var account = Account{Powerlevel: 9001}

        testUser.Name = "Jim Bob"
        testUser.Car = car
        testUser.Account = account

        // Save the user
        db.Create(&testUser)

        // Get a new user
        var newUser User


        // override db instance to always prefetch
        //db = db.Set("gorm:auto_preload", true)
        db.Set("gorm:auto_preload", true).First(&newUser)

        println(newUser.Name)
        println(newUser.Car.Type)
        println(newUser.Account.Powerlevel)

        // Explicitly preload

        // No need to use two seperate statements, you can just chain them :)
        db.Preload("Car").Preload("Account").First(&newUser)
        //db.Preload("Account").First(&newUser)

        println(newUser.Name)
        println(newUser.Car.Type)
        println(newUser.Account.Powerlevel)
}

Output:

(/home/william/main.go:46)
[2018-06-15 08:56:53]  [0.34ms]  INSERT INTO "users" ("created_at","updated_at","deleted_at","name") VALUES ('2018-06-15 08:56:53','2018-06-15 08:56:53',NULL,'Jim Bob')
[1 rows affected or returned ]

(/home/william/main.go:46)
[2018-06-15 08:56:53]  [0.11ms]  INSERT INTO "cars" ("created_at","updated_at","deleted_at","type","user_id") VALUES ('2018-06-15 08:56:53','2018-06-15 08:56:53',NULL,'1994','9')
[1 rows affected or returned ]

(/home/william/main.go:46)
[2018-06-15 08:56:53]  [0.10ms]  INSERT INTO "accounts" ("created_at","updated_at","deleted_at","powerlevel","user_id") VALUES ('2018-06-15 08:56:53','2018-06-15 08:56:53',NULL,'9001','9')
[1 rows affected or returned ]

(/home/william/main.go:51)
[2018-06-15 08:56:53]  [0.69ms]  SELECT * FROM "users"  WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" ASC LIMIT 1
[1 rows affected or returned ]

(/home/william/main.go:51)
[2018-06-15 08:56:53]  [1.93ms]  SELECT * FROM "cars"  WHERE "cars"."deleted_at" IS NULL AND (("user_id" IN ('1'))) ORDER BY "cars"."id" ASC 
[1 rows affected or returned ]
Jim Bob
1994 <-- notice here preload is on
0  <-- notice here preload was turned off Account Type

(/home/william/main.go:59)
[2018-06-15 08:56:53]  [0.28ms]  SELECT * FROM "users"  WHERE "users"."deleted_at" IS NULL AND "users"."id" = '1' ORDER BY "users"."id" ASC LIMIT 1
[1 rows affected or returned ]

(/home/william/main.go:59)
[2018-06-15 08:56:53]  [0.33ms]  SELECT * FROM "cars"  WHERE "cars"."deleted_at" IS NULL AND (("user_id" IN ('1'))) ORDER BY "cars"."id" ASC 
[1 rows affected or returned ]

(/home/william/main.go:60)
[2018-06-15 08:56:53]  [0.27ms]  SELECT * FROM "users"  WHERE "users"."deleted_at" IS NULL AND "users"."id" = '1' ORDER BY "users"."id" ASC LIMIT 1
[1 rows affected or returned ]

(/home/william/main.go:60)
[2018-06-15 08:56:53]  [0.46ms]  SELECT * FROM "accounts"  WHERE "accounts"."deleted_at" IS NULL AND (("user_id" IN ('1'))) ORDER BY "accounts"."id" ASC
[1 rows affected or returned ]

(/home/william/main.go:60)
[2018-06-15 08:56:53]  [0.40ms]  SELECT * FROM "cars"  WHERE "cars"."deleted_at" IS NULL AND (("user_id" IN ('1'))) ORDER BY "cars"."id" ASC 
[1 rows affected or returned ]
Jim Bob
1994
9001


来源:https://stackoverflow.com/questions/50873996/auto-preloading-with-gorm-not-working-as-expected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!