In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces golang gorm's Callbacks transaction rollback object how to create the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this golang gorm Callbacks transaction rollback object how to create the article will have a harvest, let's take a look.
1. Callbacks
You can define the callback method as a pointer to the model structure, which will be called when creating, updating, querying, and deleting. If any callback returns an error, gorm will stop future operations and roll back all changes.
1.1. Create object
Callbacks available during creation
/ / begin transaction start transaction BeforeSaveBeforeCreate// save before associations association / / update timestamp `CreatedAt`, `UpdatedAt` update `CreatedAt`, `UpdatedAt` timestamp / / save self save yourself / / reload fields that have default value and its value is blank reload fields with default values and empty values / / save after associations is associated with AfterCreateAfterSave// commit or rollback transaction submission or rollback transaction 1.2 after saving. Update object
Callbacks available during update
/ / begin transaction start transaction BeforeSaveBeforeUpdate// save before associations Associate / / update timestamp `UpdatedAt` update the `UpdatedAt` timestamp / / save self save yourself / / save after associations after saving associated AfterUpdateAfterSave// commit or rollback transaction commit or rollback transaction 1.3. Delete object
Callbacks available during deletion
/ / begin transaction starts transaction BeforeDelete// delete self deletes its own AfterDelete// commit or rollback transaction commit or rolls back transaction 1.4. Query object
Callbacks available during query
/ / load data from database load data from database / / Preloading (edger loading) preload (load) AfterFind1.5. Callback sample func (u * User) BeforeUpdate () (err error) {if u.readonly () {err = errors.New ("readonly user")} return} / / if the user ID is greater than 1000, roll back and insert func (u * User) AfterCreate () (err error) {if (u.Id > 1000) {err = errors.New ("user id is already greater than 1000")} return}
The save / delete operation in gorm is running in a transaction, so changes made in that transaction are not visible unless committed. If you want to use these changes in the callback, you need to run SQL in the same transaction. So you need to pass the current transaction to the callback, like this:
Func (u * User) AfterCreate (tx * gorm.DB) (err error) {tx.Model (u) .Update ("role", "admin") return} func (u * User) AfterCreate (scope * gorm.Scope) (err error) {scope.DB (). Model (u) .Update ("role", "admin") return} the article "how to create Callbacks transaction rollback objects for golang gorm" ends here. Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to create golang gorm Callbacks transaction rollback objects". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.