Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork14
Soft delete with unix seconds
License
go-gorm/soft_delete
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
https://gorm.io/docs/delete.html#Soft-Delete
import"gorm.io/plugin/soft_delete"typeUserstruct {IDuintNamestringDeletedAt soft_delete.DeletedAt}// QuerySELECT*FROMusersWHEREdeleted_at=0;// DeleteUPDATEusersSETdeleted_at=/* current unix second */WHEREID=1;
We now support ms and ns timestamp when filling thedeleted_at col, only need to specify taggorm:"softDelete:milli" orgorm:"softDelete:nano".
typeUserstruct {IDuintNamestringDeletedAt soft_delete.DeletedAt`gorm:"softDelete:milli"`// DeletedAt soft_delete.DeletedAt `gorm:"softDelete:nano"`}// QuerySELECT*FROMusersWHEREdeleted_at=0;// DeleteUPDATEusersSETdeleted_at=/* current unix milli second or nano second */WHEREID=1;
flag mode will use0,1 to mark data as deleted or not,1 means deleted
typeUserstruct {IDuintNamestringIsDel soft_delete.DeletedAt`gorm:"softDelete:flag"`}// QuerySELECT*FROMusersWHEREis_del=0;// DeleteUPDATEusersSETis_del=1WHEREID=1;
mixed mode will use0,1 to mark data as deleted or not,1 means deleted, and store delete time
typeUserstruct {IDuintNamestringDeletedAt time.TimeIsDel soft_delete.DeletedAt`gorm:"softDelete:flag,DeletedAtField:DeletedAt"`}// QuerySELECT*FROMusersWHEREis_del=0;// DeleteUPDATEusersSETis_del=1,deleted_at=/* current unix second */WHEREID=1;
support mixed mode specify time unit, e.g.gorm:"softDelete:flag,DeletedAtField:DeletedAt" orgorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli" orgorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:nano".
typeUserstruct {IDuintNamestringDeletedAtint64IsDel soft_delete.DeletedAt`gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli"`}// QuerySELECT*FROMusersWHEREis_del=0;// DeleteUPDATEusersSETis_del=1,deleted_at=/* current unix milli second second*/WHEREID=1;
This allows you to record the original ID of a deleted record in another field. By doing so, you can maintain the integrity of unique keys by allowing new records with the same unique key to be inserted without conflict.
Assume we have a User model where the Email field needs to be unique. By storing the original ID in the DeletedId field and creating a composite unique key with Email and DeletedId, you can insert a new record without violating the unique constraint even after soft deleting an existing record.
typeUserstruct {IDuintNamestringEmailstringDeletedIduint// Stores the original ID of the deleted recordIsDel soft_delete.DeletedAt`gorm:"softDelete:flag,DeletedIDField:DeletedId,DeletedIDFromField:ID"`// use `1` `0`}// QuerySELECT*FROMusersWHEREis_del=0;// DeleteUPDATEusersSETis_del=1,deleted_id=/* value from ID */WHEREID=1;
About
Soft delete with unix seconds
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.