Search code examples
postgresqlgolibpq

db.QueryRow() returning the wrong number of arguments


I have a file called user.go, the function GetUserByUsernameOrEmail returns the user model and an error. It grabs this user information from the database.

My goal is to get all parameters I SELECT of the user, but it returns one fewer than it should.

This is for a REST API, however the model itself is really the problem. The user has fields user_id uuid, user_username string, user_email string, user_hash string, user_salt string, user_verified bool, user_admin bool, user_email_verified bool.

I haven't tried much as I'm at a loss for steps to continue. To debug, however, I created 8 test variables test, test2...test8. I print all 8 variables (for more on this, see below).

    var test string
    var test2 string
    var test3 string
    var test4 string
    var test5 string
    var test6 string
    var test7 string
    var test8 string

    err = db.QueryRow(`
        SELECT 
            user_id, 
            user_username, 
            user_email, 
            user_hash,
            user_salt
            user_verified,
            user_admin,
            user_email_verified 
        FROM users
        WHERE user_`+field+`= $1
    `, value).Scan(&test, &test2, &test3, &test4, &test5, &test6, &test7, &test8)

Expected: I scan the 8 fields to my 8 variables, and go on with my day.
Actual: 2019/11/10 10:36:42 sql: expected 7 destination arguments in Scan, not 8

Some other things to note I did scan it to 7 variables to test, and got this when logging the 7 variables:

5e5d2f4a-1f5b-418d-a262-a63c719a3ea4 test test@[domain].com [hash] [salt] false false

Solution

  • Oops - I missed a commma between user_salt and user_verified. Thanks mkopriva for the help.