Last active
July 26, 2020 20:14
-
-
Save punmechanic/ebdf0ff517accf4e6e182e976904c075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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