Inserting Array of Time in Postgres via Gorm

1.2k views Asked by At

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.

1

There are 1 answers

1
Fabian On

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.