I am using gorm. I want to insert value while not exist same value
just like the raw sql.
INSERT INTO student (firstname, lastname)
SELECT 'NEW FIRSTNAME', 'NEW LASTNAME'
FROM DUAL
WHERE NOT EXISTS(
SELECT 1
FROM table_name
WHERE firstname = 'NEW FIRSTNAME' AND lastname = 'NEW LASTNAME'
)
LIMIT 1;
How to implement this function with gorm
type Student struct {
Firstname string
Lastname string
}
func insert() {
stu :=Student{"fir","la"}
if res := DB.Create(&stu).Or("some condition"); res.Error != nil {
logrus.Println(res.Error)
}
}
when found same value i want a message or error message
Get first matched record, or initalize a new one with given conditions (only works with struct, map conditions)
db.FirstOrCreate(&user, User{Name: "non_existing"})