试图从数据库获取项目,对其进行更新,然后使用GORM将其保存回数据库。但是执行保存后,该过程将挂起。
我还尝试将这些命令包装在事务中,但仍然看到该过程在保存时挂起。
但是我注意到,如果我只是执行“首先”操作来获取项目,然后执行“保存”来保存项目,则该过程将成功完成。
这真的使我感到困惑,因为显然在GORM文档中,更新的示例正是我要在此处实现的。
文档示例
db.First(&user)
user.Name = "jinzhu 2"
user.Age = 100
db.Save(&user)
我的代码
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
}
谁能解释给我带来什么改变?是什么导致进程停止/挂起?
非常感谢。
使用postgres日志更新:
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.