From dc4369d3b6199bc7548cdbd1590988c02f2a7af9 Mon Sep 17 00:00:00 2001 From: xappyyy Date: Sun, 24 Dec 2023 13:56:18 +0700 Subject: [PATCH 1/2] fix: checkin model primary key not match --- src/domain/user.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/domain/user.go b/src/domain/user.go index 364b36d..3ae76e6 100644 --- a/src/domain/user.go +++ b/src/domain/user.go @@ -5,12 +5,11 @@ type User struct { UserName string IsUserCompleted bool CocktailID uint - Checkins []Checkin `gorm:"foreignKey:UserID;references:UserID"` + Checkins []Checkin } type Checkin struct { - Checkin uint `gorm:"primaryKey"` + CheckinID uint `gorm:"primaryKey;autoincrement"` UserID string LocationID string - User User `gorm:"foreignKey:UserID;references:UserID"` } From c920b3ab08f044cf9386fb6eec8952c06c30c174 Mon Sep 17 00:00:00 2001 From: xappyyy Date: Sun, 24 Dec 2023 13:58:33 +0700 Subject: [PATCH 2/2] fix: call new session every request --- src/server/database.go | 8 ++++---- src/server/server.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/server/database.go b/src/server/database.go index 26c4cdf..4f3caeb 100644 --- a/src/server/database.go +++ b/src/server/database.go @@ -18,7 +18,7 @@ func NewSqlHandler(db *gorm.DB) interfaces.SqlHandler { //Create obj in database func (handler *SqlHandler) Create(obj interface{}) error { - result := handler.db.Session(&gorm.Session{}).Create(obj) + result := handler.db.Create(obj) if result.Error != nil { return result.Error } @@ -27,7 +27,7 @@ func (handler *SqlHandler) Create(obj interface{}) error { //Update column of obj in database with column_name and value func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, value interface{}) error { - result := handler.db.Session(&gorm.Session{}).Model(&obj).Update(column_name, value) + result := handler.db.Model(&obj).Update(column_name, value) if result.Error != nil { return result.Error } @@ -36,7 +36,7 @@ func (handler *SqlHandler) UpdateColumn(obj interface{}, column_name string, val //Get data of object by primary key func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error { - result := handler.db.Session(&gorm.Session{}).First(obj) + result := handler.db.First(obj) if result.Error != nil { return result.Error } @@ -45,7 +45,7 @@ func (handler *SqlHandler) GetByPrimaryKey(obj interface{}) error { //Get data of object with associations func (handler *SqlHandler) GetWithAssociations(obj interface{}) error { - result := handler.db.Session(&gorm.Session{}).Preload(clause.Associations).First(obj) + result := handler.db.Preload(clause.Associations).First(obj) if result.Error != nil { return result.Error } diff --git a/src/server/server.go b/src/server/server.go index 8e9d96e..802e9c4 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -39,7 +39,7 @@ func New() *Server { fmt.Printf("Error connecting to the database: %v\n", err) os.Exit(0) } - db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema)) + db = db.Exec(fmt.Sprintf("SET search_path TO %s", config.Database.Schema)).Session(&gorm.Session{}) sqlHandler := NewSqlHandler(db)