georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.27k stars 67 forks source link

scany thinks it's scanning into a primitive type #27

Closed ghost closed 3 years ago

ghost commented 3 years ago

i have this code:

type Instance struct {
    Domain       string `sql:"domain"`
    URL          string `sql:"url"`
    ClientID     string `sql:"client_id"`
    ClientSecret string `sql:"client_secret"`
}

func GetInstance(domain string) (i *Instance, err error) {
    err = pgxscan.Get(context.Background(), pool, &i, "select * from instances where domain = $1", domain)
    if errors.Is(err, pgx.ErrNoRows) {
        err = InstanceNotFound
    }
    return
}

i get the error: scany: to scan into a primitive type, columns number must be exactly 1, got: 4 what am I doing wrong here? this error doesn't seem correct (a struct is not a primitive type), and i'm pretty sure i'm using similar code (just with a different struct) elsewhere

ghost commented 3 years ago

oh, I used pointers incorrectly. the fix was to use i Instance still seems like an odd error message, tbh

georgysavva commented 3 years ago

Hi, @dev-kittens. Thanks for opening this issue. Yes, you are passing a pointer to a pointer. You need to remove the extra pointer. BTW, I can see that you use sql struct tag, JFYI scany supports only db struct tag. Mapping won't work if you use sql word.

ghost commented 3 years ago

scany supports only db struct tag ohh, this fixes some other issues i was having. thank you 👍