Mark Fletcher
Mark Fletcher

Reputation: 751

pq.Int64Array not loaded by Gorm

Using gorm to do a table scan of a postgres table, using libpq, it is not loading a pq.Int64Array.

Simplified model:

type Event struct {
    ExcludeDates pq.Int64Array // Array of StartTimes of single dates that have been deleted
}

Code to do the table scan:

    events, _ := gormdb.Model(&Event{}).Rows()
    defer events.Close()
    for events.Next() {
        var event Event
        gormdb.ScanRows(events, &event)
    }

The ExcludeDates field is always empty.

Upvotes: 3

Views: 487

Answers (1)

Mark Fletcher
Mark Fletcher

Reputation: 751

The problem was that I was using a table generated by a different ORM and it had column names that did not correspond to the naming scheme that GORM uses, so the data was not loaded into the struct properly.

You can override GORM's column naming scheme on a per-field basis, but that can be a pain if you need to override all column names. For future reference, because it does not appear to be documented, you can change the naming scheme GORM uses for column names, by redefining the gorm.TheNamingStrategy.Column function. By default GORM converts struct field names into snake case for database column names. This example instead causes GORM to convert field names into simple lowercase database column names:

    gorm.TheNamingStrategy.Column = func(in string) string {
        return strings.ToLower(in)
    }

Upvotes: 3

Related Questions