Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Soft delete with unix seconds

License

NotificationsYou must be signed in to change notification settings

go-gorm/soft_delete

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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;

Specify Time Unit

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

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

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;

Mixed Mode with Delete ID Field

Maintaining Unique Key Integrity

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.

Example

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp