go-gorm

How to save time in the database in Go when using GORM and Postgresql?

早过忘川 提交于 2019-12-11 01:46:00
问题 I'm currently parsing a time string and saving it to the db (Postgresql): event.Time, _ := time.Parse("3:04 PM", "9:00 PM") // value of event.Time now is: 0000-01-01 21:00:00 +0000 UTC db.Create(&event) It's giving me this error: pq: R:"DateTimeParseError" S:"ERROR" C:"22008" M:"date/time field value out of range: \"0000-01-01T21:00:00Z\"" F:"datetime.c" L:"3540" event.Time⁠⁠⁠⁠ 's type is time.Time . I also tried setting event.Time 's type to string and using time data type in postgresql:

Fetching NULL datetime value in MySQL using GORM

对着背影说爱祢 提交于 2019-12-10 17:36:57
问题 I want to fetch the last visit_details row having out_time as NULL using Gorm. NIL is itself a type where VisitDetail OutTime is mysql.NullTime . Code:- var visitDetail models.VisitDetail db.Where("out_time=? ", nil).Last(&visitDetail) //model VisitDetails type VisitDetail struct { Id int Visitor Visitor `gorm:"foreignkey:ClientId;association_foreignkey:Id"` VisitorId int `gorm:"not null;"` Location Location `gorm:"foreignkey:LocationId;association_foreignkey:Id"` LocationId int `gorm:"not

reflect.Value.Set using unaddressable value

半腔热情 提交于 2019-12-09 10:08:53
问题 g.GET("/", func(c echo.Context) error { var users []models.User err := db.Find(users).Error if err != nil { fmt.Println(err) } return c.JSON(http.StatusOK, users) }) this is the code for getting and displaying users from table using slice is resulting following error from gorm reflect.Value.Set using unaddressable value 回答1: You have to call Find with a pointer to the slice. err := db.Find(&users).Error relevant Gorm documentation: http://jinzhu.me/gorm/crud.html#query 回答2: Just to add

SSL connection issues between go Scratch container and PG container. How to resolve? [closed]

半城伤御伤魂 提交于 2019-12-08 14:09:00
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 12 months ago . UPDATE I believe I resolved this issue with this connection... db, err := gorm.Open("postgres", "host='postgres'&user:docker&port=5432&dbname='docker'&password='password'&sslmode=disable") I am getting a connection refused between a Docker PG container and a GoLang Scratch container. The error is: ============

time column in sqlite using gorm

廉价感情. 提交于 2019-12-08 05:33:39
问题 I am trying to query objects from sqlite but getting this error because of the type time: (sql: Scan error on column index 1: unsupported Scan, storing driver.Value type []uint8 into type *time.Time) my struct is: type Timeline struct { ID string `json:"id"` Timestamp *time.Time `json:"timestamp"` and my database is like this: CREATE TABLE timelines (id text, timestamp text, ... and one of the sample rows is: ('Locked in VR', '2018-03-17 10:50:59.548+01:00',... any ideas? should I have

How to Create or Update a record with GORM?

三世轮回 提交于 2019-12-07 11:03:58
问题 Gorm has a FirstOrCreate method and a FirstOrInit but how to check afterwards if the record was actually created? I like to create a record if it does not exists and if it exists I want to update some fields. 回答1: gormDB.Where(entity.AggregatedData{Type: v.Type}).Assign(entity.AggregatedData{Type: v.Type, Data: v.Data}).FirstOrCreate(v) SELECT * FROM "aggregated_data" WHERE ("aggregated_data"."type" = '2') ORDER BY "aggregated_data"."id" ASC LIMIT 1 and if exist then UPDATE "aggregated_data"

Why does gorm db.First() panic with “invalid memory address or nil pointer dereference”? [duplicate]

∥☆過路亽.° 提交于 2019-12-07 01:01:02
问题 This question already has answers here : How to use global var across files in a package? (2 answers) Closed yesterday . I can't figure out if I've done something silly or if I've found a bug in gorm. While I'm very well aware of what "invalid memory address or nil pointer dereference" means, I am completely mystified as to why it appears here. In short, I call db.First() and I receive a panic for no obvious reason. The relevant bits of my code: package main import ( "fmt" "github.com/gorilla

how to import gorm db connection from another file or package

人盡茶涼 提交于 2019-12-06 16:23:39
I'm learning go and I've recently learned how to leverage gorm to connect to a database. I can't figure out how to import said connection. Only open and defer it's closing in the scope of the func main() What I have currently: func main(){ db, _ := gorm.Open("postgres", "host=localhost port=5432 user=someUser dbname=someDB password=somePW sslmode=disable") defer db.Close() } This works fine and I can create tables and do CRUD ... but all in the main function. Is there anyway I can do something like this(it didn't work) and use it in main: func db(){ db, _ := gorm.Open("postgres", "host

How to Create or Update a record with GORM?

浪子不回头ぞ 提交于 2019-12-05 18:15:00
Gorm has a FirstOrCreate method and a FirstOrInit but how to check afterwards if the record was actually created? I like to create a record if it does not exists and if it exists I want to update some fields. gormDB.Where(entity.AggregatedData{Type: v.Type}).Assign(entity.AggregatedData{Type: v.Type, Data: v.Data}).FirstOrCreate(v) SELECT * FROM "aggregated_data" WHERE ("aggregated_data"."type" = '2') ORDER BY "aggregated_data"."id" ASC LIMIT 1 and if exist then UPDATE "aggregated_data" SET "data" = '[{"a":2}]', "type" = '2' WHERE "aggregated_data"."id" = '2' AND (("aggregated_data"."type" =

reflect.Value.Set using unaddressable value

你离开我真会死。 提交于 2019-12-03 14:14:44
g.GET("/", func(c echo.Context) error { var users []models.User err := db.Find(users).Error if err != nil { fmt.Println(err) } return c.JSON(http.StatusOK, users) }) this is the code for getting and displaying users from table using slice is resulting following error from gorm reflect.Value.Set using unaddressable value You have to call Find with a pointer to the slice. err := db.Find(&users).Error relevant Gorm documentation: http://jinzhu.me/gorm/crud.html#query Just to add clarification to S.Diego answers, changing this: err := db.Find(users).Error to this: err := db.Find(&users).Error the