Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql: Scan error on column index 38: destination not a pointer

Tags:

go

Using Golang and the built in database/sql library and the postgres lib/pq library, I'm trying to read from a database that has some null values in some of the records. the code compiles, but when I try to run it I get the following error.

sql: Scan error on column index 38: destination not a pointer

Here is my code:

  rows, err := db.Query(`SELECT * FROM observations WHERE profile_id=$1 AND year=$2 AND month=$3`, id, date.Year(), int(date.Month()))
  if err != nil {
    log.Fatal(err)
  }
  defer rows.Close()
  for rows.Next() {
    var id sql.NullInt64
    var year sql.NullInt64
    var month sql.NullInt64
    var day_of_week sql.NullInt64 
    var hour sql.NullInt64
    var profile_id sql.NullInt64 
    var created_at time.Time 
    var updated_at time.Time
    var banking sql.NullFloat64
    var hlt_pro sql.NullFloat64 
    var sup_shp sql.NullFloat64 
    var aff_con sql.NullFloat64 
    var biz_trv sql.NullFloat64 
    var investing sql.NullFloat64 
    var day_com sql.NullFloat64 
    var unique_emin sql.NullFloat64 
    var no_group sql.NullFloat64 
    var movie_goer sql.NullFloat64 
    var luxury_shopper sql.NullFloat64 
    var sports_fan sql.NullFloat64 
    var aland sql.NullInt64
    var awater sql.NullInt64
    var tractce sql.NullString
    var geoid sql.NullString
    var median_income sql.NullInt64 
    var total_population sql.NullInt64 
    var white sql.NullInt64 
    var black sql.NullInt64 
    var native_american sql.NullInt64 
    var asian sql.NullInt64 
    var pacific_islander sql.NullInt64 
    var other sql.NullInt64 
    var two_plus sql.NullInt64 
    var two_plus_question sql.NullInt64 
    var confused sql.NullInt64 
    var median_age sql.NullFloat64
    var count sql.NullFloat64
    var latitude sql.NullString
    var longitude sql.NullString
    if err := rows.Scan(&id, &year, &month, &day_of_week, &hour, &profile_id, &created_at, &updated_at, &banking, &hlt_pro, &sup_shp, &aff_con, &biz_trv, &investing, &day_com, &unique_emin, &no_group, &movie_goer, &luxury_shopper, &sports_fan, &aland, &awater, &tractce, &geoid, &median_income, &total_population, &white, &black, &native_american, &asian, &pacific_islander, &other, &two_plus, &two_plus_question, &confused, &median_age, &count, &latitude, longitude); err != nil {
      log.Fatal(err)
    }
    fmt.Println(id, year, month, day_of_week, hour, profile_id, created_at, updated_at, banking, hlt_pro, sup_shp, aff_con, biz_trv, investing, day_com, unique_emin, no_group, movie_goer, luxury_shopper, sports_fan, aland, awater, tractce, geoid, median_income, total_population, white, black, native_american, asian, pacific_islander, other, two_plus, two_plus_question, confused, median_age, count, latitude, longitude)

  }
  if err := rows.Err(); err != nil {
    log.Fatal(err)
  }

I'm using sql.NullType, which is the same as doing *type. This is an effort handle the null situation, and it seems to be working for most columns, but I'm not sure why it's saying "destination not a pointer". Also, I have no idea which variable is the problem. Any ideas?

like image 993
Arel Avatar asked Jul 29 '15 20:07

Arel


1 Answers

Your last parameter, longitude, is not being passed by address.

change to &longitude

as a side note, using "select *" and assuming column position is predictable is a recipe for down the road errors.

You probably should name all the columns you select, in case a future table change causes a column ordering issue.

like image 63
David Budworth Avatar answered Oct 22 '22 17:10

David Budworth