3

While trying to insert into postgres db via gorm It is unable to convert time.Time information into timestamptz.
The error message shown is:

unable to encode time.Date(2023, time.January, 8, 1, 34, 22, 4178000, time.Local) into binary format for _timestamptz (OID 1185): cannot find encode plan

I created the struct as follows:

type Instance struct {
    UpdatedAt     time.Time `gorm:"index"`
    InstanceId    string    `gorm:"primaryKey"`
    InstanceName  string
    InstanceType  string
    InstanceState string
    LaunchTime   time.Time
    ExitTime     time.Time
    PausedTimes  []time.Time `gorm:"type:timestamptz[]"`
    ResumedTimes []time.Time `gorm:"type:timestamptz[]"`
}

To Insert I tried running:

dbConnection.Clauses(clause.OnConflict{
            UpdateAll: true,
        }).Create(&model.InstanceDB{
            InstanceId:   "1",
            PausedTimes:  []time.Time{time.Now(), time.Now().Add(50000)},
            ResumedTimes: []time.Time{time.Now()},
        })

I though about using pq.Array but I couldn't find if it supports TimeArray.
Alternative approach would be to use unix timestamps using pq.Int64Array but I wanted to see if I could resolve this and keep using time.Time.

  • I also tried using *`pgtype.Timestamptz`* but doesn't seems to work still. _Structure:_ `ResumedTimes []pgtype.Timestamptz \`gorm:"type:timestamptz[]"\`` Code: ```db.Create(&model.InstanceDB{ InstanceId: "1", ResumedTimes: []pgtype.Timestamptz{{Time: time.Now(), Valid: true}} })``` The error changes to: ```ERROR: column "resumed_times" is of type timestamp with time zone[] but expression is of type record (SQLSTATE 42804)``` – Priyansh Choudhary Jan 08 '23 at 08:23

1 Answers1

0

Got the same issue. Only downgrading helped:

gorm.io/driver/postgres v1.4.4
gorm.io/gorm v1.23.7

Possibly related:

Best make a bug report.

Fabian
  • 5,040
  • 2
  • 23
  • 35