ent 生成的代码中client 提供了比较全的事务处理 启动单个事务进行处理 // GenTx generates group of entities in a transaction. func GenTx(ctx context.Context, client *ent.Client) error { tx, err := client.Tx(ctx) if err != nil { return fmt.Errorf("starting a transaction: %v", err) } hub, err := tx.Group. Create(). SetName("Github"). Save(ctx) if err != nil { return rollback(tx, fmt.Errorf("failed creating the group: %v", err)) } // Create the admin of the group. dan, err := tx.User. Create(). SetAge(29). SetName("Dan"). AddManage(hub). Save(ctx) if err != nil { return rollback(tx, err) } // Create user "Ariel". a8m, err :=