I want to set a uuid for a Model.ID I am using github.com/google/uuid
This is the code
type Quiz struct {
ID string `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt" gorm:"not null"`
Title string `json:"title" gorm:"not null"`
CreatedBy string `json:"createdBy" gorm:"not null"`
Questions []Question `json:"questions" gorm:"foreignKey:QuizID;not null;"`
}
func (q *Quiz) BeforeCreate(tx *gorm.DB) (err error) {
q.ID = uuid.New().String()
return
}
Error:-
2023/08/08 21:03:32 /home/kulothungan/quiz-maker-backend/controllers/quiz_controller.go:18 strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax; strconv.ParseUint: parsing "8f198dee-1139-4e6f-88c9-cdc5efb17762": invalid syntax
Is sqlite doesn't support this or what? Please help me
Comment From: keyvangholami
SQLite in its own right lacks a native UUID data type. Nevertheless, it is possible to store UUIDs in the form of text. It seems that the issue at hand is that GORM anticipates the primary key to be an integer (specifically an unsigned int), whereas you have supplied a UUID in string format.
Comment From: twocs
I'm able to use primary key of string, so I don't suspect that's the problem.
The error presented appears on this line of code:
/home/kulothungan/quiz-maker-backend/controllers/quiz_controller.go:18
This is your code, not Gorm's, so to diagnose the problem better, I'd like to see that line of code!
Comment From: Mcklmo
@twocs I'm facing the same issue. Using gorm with a postgres instance. I'm using custom migration scripts, but I include a copy of the database schema below for reproducability.
Struct:
User struct {
gorm.Model `json:"-"`
ID uuid.UUID `json:"ID" gorm:"type:uuid"`
// other fields
}
copy of users schema:
Table "public.users"
Column | Type | Collation | Nullable | Default
-------------------------------+--------------------------+-----------+----------+-------------------
id | uuid | | not null | gen_random_uuid()
// other unrelated fields
code:
func (r userRepository) Update(user chat.User) (chat.User, error) {
if err := r.db.Where("id = ?", user.ID).Updates(&user).Scan(&user).Error; err != nil {
return user, err
}
return user, nil
}
Error:
2024/01/13 21:17:06 d:/GitHub/BCV/besammen/be-sammen-backend/internal/repository/user.go:67 strconv.ParseUint: parsing "fddbd108-448f-4031-aaee-9c7decede2a9": invalid syntax; strconv.ParseUint: parsing "fddbd108-448f-4031-aaee-9c7decede2a9": invalid syntax
[3.320ms] [rows:1] UPDATE "users" SET "email"='iixnjgbjipvvdem@gmail.com',"created_at"='2024-01-13 21:16:55.525',"updated_at"='2024-01-13 21:17:06.146',"notification_on_message"=true,"notification_weekly"=true,"password"='$2a$10$PERGe4aFYgSOsHvy90aeIOMhkKZ3OsrNEeIb/5d.lToJO6bajKrKm' WHERE id = 'fddbd108-448f-4031-aaee-9c7decede2a9' AND "users"."deleted_at" IS NULL AND "id" = 'fddbd108-448f-4031-aaee-9c7decede2a9'
2024/01/13 21:17:06 d:/GitHub/BCV/besammen/be-sammen-backend/internal/repository/user.go:67 strconv.ParseUint: parsing "fddbd108-448f-4031-aaee-9c7decede2a9": invalid syntax; strconv.ParseUint: parsing "fddbd108-448f-4031-aaee-9c7decede2a9": invalid syntax
Weird observation: The id is checked twice in the executed sql WHERE clause, possibly the reason for the duplicated error message.
Comment From: Mcklmo
Nevermind, I had referenced the user struct from another struct without realising, causing the error. However, it would be cool if the error was more clear.
Edit: for clarification, the other struct had a uint field that referenced the user struct. Attempted updates to the user struct in the database caused the error. Gorm should be more expressive about that error.
Comment From: kulothunganug
I'm able to use primary key of string, so I don't suspect that's the problem. The error presented appears on this line of code:
/home/kulothungan/quiz-maker-backend/controllers/quiz_controller.go:18This is your code, not Gorm's, so to diagnose the problem better, I'd like to see that line of code!
I don't have that code now, however I tried to replicate the same and it works fine... Sorry for inconvenience
Here's the code I tested with