Time Maintenance and Soft Deletion
In practical applications, database tables often include three timestamp fields: creation time, update time, and delete time. GoFrame supports automatic population of these three time fields, which can be of types DATE
, DATETIME
, or TIMESTAMP
.
- Creation Time: Default field name is
created_at
. - Update Time: Default field name is
updated_at
. - Delete Time: Default field name is
deleted_at
, used for soft deletion.
If you prefer not to use the default field names, you can customize them in the database configuration section of your configuration file, as follows:
database:
....
createdAt: "create_time"
updatedAt: "update_time"
deletedAt: "delete_time"
Soft Deletion
Soft deletion does not physically remove records from the database but marks them with a specific flag to filter them out from queries, making them invisible on the application side while still existing in the database. It is commonly used for data that needs to be tracked historically and cannot be permanently deleted.
When your data table includes a deleted_at
field, using the Delete
method will not physically remove the data but will instead update the deleted_at
field. When querying data, GoFrame automatically adds a WHERE `deleted_at` IS NULL condition to filter out the "deleted" data.
To query all data, including those marked as deleted, you need to use the Unscoped
method:
list, _ := md.Unscoped().All()
This method retrieves all records from the database table, including those that have been "soft deleted".