Was trying to get an item from db, updates it and save it back to db using GORM. but the process hangs after doing the save.
I also tried to wrap these commands in a transaction, but still see the process hangs at save.
However I noticed if I just do "First" to get the item and "Save" to save back the item, the process completes successfully.
This really puzzles me because apparently in the GORM documentation, the example of an update is exactly what I am trying to achieve here.
Example in doc
db.First(&user)
user.Name = "jinzhu 2"
user.Age = 100
db.Save(&user)
My code
type Item struct {
PK int `json:"-" gorm:"index;primary_key"`
ID string `json:"id" gorm:"index" external_reference_key:"true"`
MEMO string `json:"MEMO"`
}
func main() {
db, err := gorm.Open("postgres", "host=localHost port=5432 user=postgres password=5206 dbname=item sslmode=disable")
if err != nil {
panic(err)
}
db.LogMode(true)
item := Item{}
db.First(&item) //Gets the item
item.MEMO = "[hpapaapodfiso]" //Change the field
db.Save(&item) //Save back to db
fmt.Printf("%v",item) //never executed, process stops at Save
}
Could anyone explain to me what made a difference? And what caused the process to stop/hang?
Really appreciated.
Update with the postgres log:
2019-06-22 15:39:07 PDT LOG: database system is ready to accept connections
2019-06-22 15:39:08 PDT LOG: autovacuum launcher started
2019-06-22 15:40:08 PDT FATAL: canceling authentication due to timeout
2019-06-22 15:49:23 PDT LOG: could not receive data from client: No connection could be made because the target machine actively refused it.
2019-06-22 15:53:22 PDT LOG: could not receive data from client: No connection could be made because the target machine actively refused it.
2019-06-22 15:53:31 PDT LOG: could not receive data from client: No connection could be made because the target machine actively refused it.
2019-06-22 15:53:46 PDT LOG: could not receive data from client: No connection could be made because the target machine actively refused it.
2019-06-22 16:05:42 PDT ERROR: role "username" does not exist
2019-06-22 16:05:42 PDT STATEMENT: REVOKE CONNECT ON DATABASE triggers FROM PUBLIC, username;
2019-06-22 16:06:14 PDT FATAL: terminating connection due to administrator command
2019-06-22 16:06:14 PDT FATAL: terminating connection due to administrator command
2019-06-22 16:06:14 PDT LOG: could not send data to client: No connection could be made because the target machine actively refused it.
2019-06-22 16:06:14 PDT FATAL: terminating connection due to administrator command
2019-06-22 16:06:14 PDT LOG: could not send data to client: No connection could be made because the target machine actively refused it.
2019-06-22 16:06:14 PDT FATAL: terminating connection due to administrator command
2019-06-22 16:06:14 PDT LOG: could not send data to client: No connection could be made because the target machine actively refused it.
2019-06-22 16:06:14 PDT LOG: could not send data to client: No connection could be made because the target machine actively refused it.