8treenet / gcache

gcache是gorm的中间件,插入后gorm即刻拥有缓存。
Apache License 2.0
386 stars 52 forks source link

Delete got panic: reflect.Value.Addr of unaddressable value #17

Closed sutra closed 4 years ago

sutra commented 4 years ago

测试代码如下:

func TestAssociation(t *testing.T) {
    var tc TestUser
    db.First(&tc, "user_name = ?", "name_1")
    assert.Equal(t, "name_1", tc.UserName)
    assert.Equal(t, 0, len(tc.Roles))

    cachePlugin.SkipCache().Preload("Roles").Find(&tc)
    assert.Equal(t, 2, len(tc.Roles))

    db.Preload("Roles").Find(&tc)
    assert.Equal(t, 2, len(tc.Roles))

    var adminRole TestRole
    db.Model(&adminRole).Where("name = ?", "ADMIN").First(&adminRole)
    assert.Equal(t, "ADMIN", adminRole.Name)

    db.Model(tc).Association("Roles").Delete(adminRole) // panic: reflect.Value.Addr of unaddressable value
}

这个删除操作会导致 panic: reflect.Value.Addr of unaddressable value,测试了去掉 gcache 是能通过测试的。

Other code see #16, full code see https://github.com/8treenet/gcache/compare/master...sutra:testAssociation?expand=1

8treenet commented 4 years ago

不支持Association,先skip下。等我处理下Association跳过

杨树 Shu Yang Tel: +8613513517944 Twitter: _CharlieYang Website: http://www.8tree.net

------------------ Original ------------------ From: Sutra Zhou <notifications@github.com> Date: Tue,Sep 22,2020 9:47 PM To: 8treenet/gcache <gcache@noreply.github.com> Cc: Subscribed <subscribed@noreply.github.com> Subject: Re: [8treenet/gcache] Association got panic: runtime error: invalid memory address or nil pointer dereference (#17)

func TestAssociation(t *testing.T) { var tc TestUser db.First(&tc, "user_name = ?", "name_1") assert.Equal(t, "name_1", tc.UserName) assert.Equal(t, 0, len(tc.Roles)) cachePlugin.SkipCache().Preload("Roles").Find(&tc) assert.Equal(t, 2, len(tc.Roles)) db.Preload("Roles").Find(&tc) assert.Equal(t, 2, len(tc.Roles)) var adminRole TestRole db.Model(&adminRole).Where("name = ?", "ADMIN").First(&adminRole) assert.Equal(t, "ADMIN", adminRole.Name) db.Association("Roles") // panic: runtime error: invalid memory address or nil pointer dereference }
Other code see #16, full code see https://github.com/8treenet/gcache/compare/master...sutra:testAssociation?expand=1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

sutra commented 4 years ago

那能否有一种机制,默认不开启 gcache,只在需要时开启它呢?我在现有工作良好的代码上增加 gcache,如果 100% 覆盖地去测试太困难了,希望能默认不开启,只在需要的地方开启,这样我可以针对性地对开启了的地方进行测试。

8treenet commented 4 years ago

你说的这个需求可以加。不难。明天我一起处理下。 常用的没有问题。外键的这几个用的很少。所以我也没覆盖到

8treenet commented 4 years ago

修复了v1.1.10