56312

time column in sqlite using gorm

Question:

I am trying to query objects from sqlite but getting this error because of the type time:

(sql: Scan error on column index 1: unsupported Scan, storing driver.Value type []uint8 into type *time.Time)

my struct is:

type Timeline struct { ID string `json:"id"` Timestamp *time.Time `json:"timestamp"`

and my database is like this:

CREATE TABLE timelines (id text, timestamp text, ...

and one of the sample rows is:

('Locked in VR', '2018-03-17 10:50:59.548+01:00',...

any ideas? should I have something in the struct like?

Timestamp *time.Time `json:"timestamp" gorm:"time"`

Answer1:

I am not familiar with gorm, but should not the definition of timestamp of type datetime instead of text? Also: when you tag gorm:"time" the column name should be time and not timestamp, or the tag gorm:"timestamp". But you can leave out the gorm tag.

To make it simple, you can let gorm create the table:

db, err := gorm.Open("sqlite3", "test.db") db.CreateTable(&Timeline{})

Answer2:

Using this would take care of it:

type Timeline struct { ID string `json:"id"` Timestamp *time.Time `json:"timestamp" gorm:"type:datetime"` }

You could even change the declared type of the Timestamp field to something else, say int64 to represent Unix times. Then you could write a Scanner to read the datetime field into the int64 field.

type TimeStampUnix int64 type Timeline struct { ID string `json:"id"` TimeStamp TimeStampUnix `json:"timestamp" gorm:"type:datetime"` } func (t *TimeStampUnix) Scan(src interface{}) error { switch src.(type) { case time.Time: *t = TimeStampUnix(src.(time.Time).Unix()) return nil case []byte: // bonus code to read text field of format '2014-12-31 14:21:01-0400' // str := string(src.([]byte)) var y, m, d, hr, min, s, tzh, tzm int var sign rune _, e := fmt.Sscanf(str, "%d-%d-%d %d:%d:%d%c%d:%d", &y, &m, &d, &hr, &min, &s, &sign, &tzh, &tzm) if e != nil { return e } offset := 60 * (tzh*60 + tzm) if sign == '-' { offset = -1 * offset } loc := time.FixedZone("local-tz", offset) t1 := time.Date(y, time.Month(m), d, hr, min, s, 0, loc) *t = TimeStampUnix(t1.Unix()) return nil default: return fmt.Errorf("Value '%s' of incompatible type '%T' found", string(src.([]byte)), src) } }

Recommend

  • jQuery UI confirmation dialog - manipulating output
  • Unable to remove zipped file after unzipping
  • Why can't I use Set:union() instead of Set.union?
  • Node.js fs.unlink function causes EPERM error
  • Old skype issues [closed]
  • cannot kill redis-server on linux
  • Possible to hide app icon in iOS?
  • Inheritance and lazy loading in NHibernate
  • spark in python: creating an rdd by loading binary data with numpy.fromfile
  • Metro Flyouts with MVVM
  • Preventing Subview from Transforming when its Superview Transforms
  • Apple Push Notification Bundle Name
  • Django email message as HTML
  • Add missing rows within combinations of factors
  • Ioniv-v4 : ionic cordova plugin add cordova-plugin-googleplus Not working
  • Excel: Cell counts based conditions
  • How to link address model to views
  • performance counter events associated with false sharing
  • python pandas isin method?
  • Converting a self subquery to a self join
  • Trigger powershell based on event log
  • Python tk scrollbar becomes inactive once text is outside the screen
  • IE doesn't display png images
  • Jenkins sending notifications to the wrong commit id
  • yii rewrite url with many sub categories
  • Load php page results into div with ajax with link?
  • Adding horizontal slider to QTableWidget
  • Java Collections.shuffle() weird behaviour [closed]
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to change user identity when git pushing via ssh?
  • How to warp text around image in iOS?
  • Update cell query for Excel ADO from Delphi
  • Accessing Arguments, Workflow Variables from custom activities