0

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.
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
user2891374
  • 21
  • 2
  • 4
  • maybe there is an error in your db calls? Check using `db.First(&item).Error` and `db.Save(&item).Error` – Mario Pérez Alarcón Jun 22 '19 at 20:11
  • db.First(&item).Error returns nil, and db.Save(&item) fails at ```go newDB := scope.callCallbacks(s.parent.callbacks.updates).db ``` so the .Error could not be reached. – user2891374 Jun 22 '19 at 20:55
  • Could you post the whole stack error in this question? I think it'll be easier for checking what is going on – Mario Pérez Alarcón Jun 23 '19 at 08:30
  • There is no error log from debugging the app, which is why it is so hard to figure out why. I had to kill my app several times during the debug because the app hangs. Updated the issue with postgres db log. – user2891374 Jun 24 '19 at 18:10
  • I guess this is more about having the right setup with postgres. Maybe this helps: https://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist – Mario Pérez Alarcón Jun 25 '19 at 08:53

0 Answers0