Create
user := User{}
result := db.Create(&user)
Retrieve
db.Where("name <> ?", "jinzhu").Find(&users)
db.Where("name <> ?", "jinzhu").First(&user)
db.Where("name <> ?", "jinzhu").Last(&user)
Update
Single Column
db.Model(&user).Update("name", "hello")
Multiple Column
// Update attributes with `struct`, will only update non-zero fields
db.Model(&user).Updates(User{Name: "hello", Age: 18, Active: false})
Delete
db.Where("name = ?", "jinzhu").Delete(&email)
Soft Delete
If the table contains
deleted_at
, it will only update it as soft deleting.// Batch Delete
db.Where("age = ?", 20).Delete(&User{})
// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20;