https://github.com/go-gorm/playground/pull/1
Description
When not using gorm.Model as the base struct, the method First gets confused when no columns are specified.
The expected behavior should be that if an ID column exists, First should keep working like normal with the gorm.Model. Instead it only works by specifing the column to search for.
I'm using gorm with the mysql driver, and the specified parameter is an UUID v4 generated from fiber.utils as a string.
type BaseModel struct {
ID string `json:"ID" gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (b *BaseModel) BeforeCreate(tx *gorm.DB) error {
b.ID = utils.UUIDv4()
return nil
}
type User struct {
BaseModel
Name string `json:"Name" gorm:"notnull"`
Username string `json:"Username" gorm:"notnull;unique"`
Email string `json:"-" gorm:"notnull;unique"`
}
---
func get() {
var user User
// doesnt'work
database.DBConn.First(&user, "d2a5beec-7cc4-4671-881a-d7b28b2719c2")
// works
database.DBConn.First(&user, "id = ?", "d2a5beec-7cc4-4671-881a-d7b28b2719c2")
}
Error:
Error 1054: Unknown column 'd2a5beec' in 'where clause'
SELECT * FROM `users` WHERE d2a5beec-7cc4-4671-881a-d7b28b2719c2 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: nicolasvac
Issue is active.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: minayousseif
I think it was mentioned in the docs Retrieving-objects-with-primary-key
Objects can be retrieved using primary key by using Inline Conditions if the primary key is a number, Be extra careful with strings to avoid SQL Injection, check out Security section for details
If primary key is a string like uuid, need to write it like:
db.First(&user, "id = ?", "1b74413f-f3b8-409f-ac47-e8c062e3472a")
// SELECT * FROM users WHERE id = "1b74413f-f3b8-409f-ac47-e8c062e3472a";
Comment From: LuisAntezana
This also works if you don't want to use the SQL syntax:
var user = User{ID: "1b74413f-f3b8-409f-ac47-e8c062e3472a"}
database.DBConn.First(&user)
Comment From: semmet95
Thanks to @minayousseif and @LuisAntezana You guys saved me so much time.