Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Last active July 26, 2020 20:14
Show Gist options
  • Save punmechanic/ebdf0ff517accf4e6e182e976904c075 to your computer and use it in GitHub Desktop.
Save punmechanic/ebdf0ff517accf4e6e182e976904c075 to your computer and use it in GitHub Desktop.
panic: invalid sql type AttributeMap (map) for sqlite3
goroutine 1 [running]:
github.com/jinzhu/gorm.(*sqlite3).DataTypeOf(0xc0001a05c0, 0xc0000e9e00, 0x6, 0x6d68b8)
/home/dan/gopath/pkg/mod/github.com/jinzhu/[email protected]/dialect_sqlite3.go:64 +0x751
github.com/jinzhu/gorm.(*Scope).createTable(0xc0001a2180, 0xc000192698)
/home/dan/gopath/pkg/mod/github.com/jinzhu/[email protected]/scope.go:1173 +0x27c
github.com/jinzhu/gorm.(*Scope).autoMigrate(0xc0001a2180, 0x683640)
/home/dan/gopath/pkg/mod/github.com/jinzhu/[email protected]/scope.go:1269 +0x400
github.com/jinzhu/gorm.(*DB).AutoMigrate(0xc000197860, 0xc0000d9f40, 0x1, 0x1, 0x1)
/home/dan/gopath/pkg/mod/github.com/jinzhu/[email protected]/main.go:689 +0x96
main.main()
/home/dan/main.go:37 +0x103
exit status 2
package main
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Record struct {
Attributes AttributeMap
}
// AttributeMap is a map of attributes that is encoded to and from JSON within the database.
type AttributeMap map[string]interface{}
func (a *AttributeMap) Value() (driver.Value, error) {
return json.Marshal(*a)
}
func (a *AttributeMap) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
// Wasn't bytes!
return fmt.Errorf("expected bytes, received %T", value)
}
return json.Unmarshal(b, a)
}
func main() {
db, err := gorm.Open("sqlite3", ":memory:")
must(err)
must(db.AutoMigrate(&Record{}).Error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment