GORM Playground Link

https://github.com/go-gorm/playground/pull/506

Description

In v1.21.15 I was able to Scan into a pointer to a UUID, which is a type alias for [16]byte. This now panics in v1.23.8. I want to scan into the pointer so that the nil value represents the record not existing. Debugging the issue, it looks as if the code attempts to call reflect.MakeSlice on an array type, which leads to the panic.

Comment From: a631807682

I tried it in v1.21.15 and it didn't work. The problem here is that if the user scan [16]byte, we don't know if he wants to query multiple records or one record.

Comment From: chradcliffe

I updated the playground PR to be closer to what we have in our actual code -- the Where clause seems to make a difference.

Comment From: a631807682

Pointer is unwrapped https://github.com/go-gorm/gorm/blob/master/finisher_api.go#L543 it make scan pointer in pointer possible https://github.com/go-gorm/gorm/blob/master/tests/scan_test.go#L36 So the problem still exists

The problem here is that if the user scan [16]byte, we don't know if he wants to query multiple records or one record.

Comment From: chradcliffe

The problem here is that if the user scan [16]byte, we don't know if he wants to query multiple records or one record.

I agree that it's not clear what the expected behaviour should be. Maybe it would be best to return an error when Scan is passed an array that isn't wrapped in a slice or struct (and document the restriction)? Not sure if that would break API guarantees though.

fwiw I'm working around the issue by wrapping the UUID in a struct, e.g.:

    var result struct {
        MyID *uuid.UUID
    }
    DB.Table("foo").Select("my_id").Where("bar = 12").Scan(&result)

Comment From: a631807682

Maybe it would be best to return an error when Scan is passed an array that isn't wrapped in a slice or struct (and document the restriction)? Not sure if that would break API guarantees though.

We can't support both at the same time, so we chose one ( guess user always query multiple records ), unfortunately you belong to the other, sorry you can only wrap the UUID in a struct. I think this does have to be stated in the documentation, @chradcliffe are you interested in helping us improve it?