banner



Does Primary Key Automatically Update If You Delete A Row

Delete

Delete a Tape

When deleting a tape, the deleted value needs to have primary key or it will trigger a Batch Delete, for example:

                                        
db.Delete(&email)



db.Where("proper name = ?", "jinzhu").Delete(&email)

Delete with principal key

GORM allows to delete objects using chief key(south) with inline status, it works with numbers, check out Query Inline Weather condition for details

                    db.Delete(&User{},                      ten)                    


db.Delete(&User{}, "10")


db.Delete(&users, []int{1,2,3})

Delete Hooks

GORM allows hooks BeforeDelete, AfterDelete, those methods will exist chosen when deleting a tape, refer Hooks for details

                                                                  func                        (u *User)                                            BeforeDelete(tx *gorm.DB) (err                      error) {                    
if u.Role == "admin" {
render errors.New("admin user not immune to delete")
}
return
}

Batch Delete

The specified value has no primary value, GORM will perform a batch delete, it will delete all matched records

                    db.Where("e-mail Similar ?",                      "%jinzhu%").Delete(&Email{})                    


db.Delete(&Email{}, "email LIKE ?", "%jinzhu%")

Cake Global Delete

If yous perform a batch delete without any weather, GORM WON'T run it, and will return ErrMissingWhereClause error

Yous accept to apply some weather or use raw SQL or enable AllowGlobalUpdate fashion, for example:

                    db.Delete(&User{}).Mistake                                        

db.Where("1 = 1").Delete(&User{})


db.Exec("DELETE FROM users")


db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&User{})

Returning Data From Deleted Rows

Return deleted data, only works for database support Returning, for instance:

                                        
var users []User
DB.Clauses(clause.Returning{}).Where("role = ?", "admin").Delete(&users)




DB.Clauses(clause.Returning{Columns: []clause.Cavalcade{{Proper name: "proper name"}, {Proper noun: "salary"}}}).Where("role = ?", "admin").Delete(&users)


Soft Delete

If your model includes a gorm.DeletedAt field (which is included in gorm.Model), information technology volition get soft delete ability automatically!

When calling Delete, the record WON'T exist removed from the database, merely GORM will set up the DeletedAt'southward value to the current fourth dimension, and the data is not findable with normal Query methods anymore.

                                        
db.Delete(&user)



db.Where("age = ?", 20).Delete(&User{})



db.Where("age = 20").Observe(&user)

If y'all don't want to include gorm.Model, you can enable the soft delete feature similar:

                                          type                      User                      struct                      {                    
ID int
Deleted gorm.DeletedAt
Name string
}

Find soft deleted records

Y'all can find soft deleted records with Unscoped

                    db.Unscoped().Where("historic period = 20").Find(&users)                    

Delete permanently

You tin can delete matched records permanently with Unscoped

                    db.Unscoped().Delete(&guild)                    

Delete Flag

Use unix second as delete flag

                                          import                      "gorm.io/plugin/soft_delete"                                        

type User struct {
ID uint
Proper noun string
DeletedAt soft_delete.DeletedAt
}


SELECT * FROM users WHERE deleted_at = 0;


UPDATE users SET deleted_at = WHERE ID = ane;

INFO when using unique field with soft delete, you should create a composite index with the unix second based DeletedAt field, e.thousand:

                                              import                        "gorm.io/plugin/soft_delete"                                            

type User struct {
ID uint
Proper noun cord `gorm:"uniqueIndex:udx_name"`
DeletedAt soft_delete.DeletedAt `gorm:"uniqueIndex:udx_name"`
}

Use one / 0 as delete flag

                                          import                      "gorm.io/plugin/soft_delete"                                        

type User struct {
ID uint
Proper noun string
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag"`
}


SELECT * FROM users WHERE is_del = 0;


UPDATE users Gear up is_del = 1 WHERE ID = 1;

GitHub tag (latest SemVer)

Source: http://gorm.io/docs/delete.html

Posted by: smithaftervare.blogspot.com

0 Response to "Does Primary Key Automatically Update If You Delete A Row"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel