<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 刪除 ## 刪除記錄 刪除一條記錄 ```go // 刪除一條已有的記錄(email 的主鍵值為 10) db.Delete(&email) // DELETE from emails where id=10; // 通過內聯條件刪除記錄 db.Delete(&Email{}, 20) // DELETE from emails where id=20; // 帶上其它條件 db.Where("name = ?", "jinzhu").Delete(&email) // DELETE FROM emails WHERE id=10 AND name = 'jinzhu' ``` ## 刪除鉤子 對于刪除操作,GORM 支持 `BeforeDelete`、`AfterDelete` 鉤子,在刪除記錄時會調用這些方法,詳情請參考 [鉤子](../教程/hooks.html) ```go func (u *User) BeforeDelete(tx *gorm.DB) (err error) { if u.Role == "admin" { return errors.New("admin user not allowed to delete") } return } ``` ## 批量刪除 如果沒有指定帶有主鍵值的記錄,GORM 將執行批量刪除,刪除所有匹配的記錄 ```go db.Where("email LIKE ?", "%jinzhu%").Delete(Email{}) // DELETE from emails where email LIKE "%jinzhu%"; db.Delete(Email{}, "email LIKE ?", "%jinzhu%") // DELETE from emails where email LIKE "%jinzhu%"; ``` ### 阻止全局刪除 如果在沒有任何條件的情況下執行批量刪除,GORM 不會執行該操作,并返回`ErrMissingWhereClause`錯誤 您可以使用 `1 = 1` 之類的條件來強制全局刪除 ```go db.Delete(&User{}).Error // gorm.ErrMissingWhereClause db.Where("1 = 1").Delete(&User{}) // DELETE `users` WHERE 1=1 ``` ## 軟刪除 如果您的模型包含了一個 `gorm.deletedat` 字段(`gorm.Model` 已經包含了該字段),它將自動獲得軟刪除的能力! 擁有軟刪除能力的模型調用 `Delete` 時,記錄不會被數據庫。但 GORM 會將 `DeletedAt` 置為當前時間, 并且你不能再通過普通的查詢方法找到該記錄。 ```go db.Delete(&user) // UPDATE users SET deleted_at="2013-10-29 10:23" WHERE id = 111; // 批量刪除 db.Where("age = ?", 20).Delete(&User{}) // UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20; // 在查詢時會忽略被軟刪除的記錄 db.Where("age = 20").Find(&user) // SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL; ``` 如果您不想引入 `gorm.Model`,您也可以這樣啟用軟刪除特性: ``` type User struct { ID int Deleted gorm.DeletedAt Name string } ``` ### 查找被軟刪除的記錄 您可以使用 `Unscoped` 找到被軟刪除的記錄 ```go db.Unscoped().Where("age = 20").Find(&users) // SELECT * FROM users WHERE age = 20; ``` ### 永久刪除 您也可以使用 `Unscoped` 永久刪除匹配的記錄 ```go db.Unscoped().Delete(&order) // DELETE FROM orders WHERE id=10; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看