Skip to content

Commit

Permalink
fix: checkin model primary key not match
Browse files Browse the repository at this point in the history
fix: checkin model primary key not match
fix: call new session every request
  • Loading branch information
xappyyy authored Dec 24, 2023
2 parents 300d034 + c920b3a commit 73820e5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
8 changes: 4 additions & 4 deletions src/server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 73820e5

Please sign in to comment.