Deleting Single Records

Deleting records is very similar to updating records, except that you'll use the Destroy method, along with the previously seen Find method to pull the exact record you want into a model.

package main

import (
    "bitbucket.org/pop-book/models"
    "fmt"
    "github.com/gobuffalo/pop"
    "log"
)

func main() {
    tx, err := pop.Connect("development")
    if err != nil {
        log.Panic(err)
    }

    id := "240ec3c5-019d-4031-9c27-8a553e022297"
    frank := models.User{}
    err = tx.Find(&frank, id)
    if err != nil {
        fmt.Print("ERROR!\n")
        fmt.Printf("%v\n", err)
    } else {
        fmt.Print("Success! - Now delete it.\n")
        tx.Destroy(&frank)
    }

    frank_test := models.User{}
    err = tx.Find(&frank_test, id)
    if err != nil {
        fmt.Print("Record not found!\n")
        fmt.Printf("%v\n", err)
    } else {
        fmt.Print("I shouldn't have found it.\n")
    }
}

Compile and run it:

$ ./main
Success! - Now delete it.
Record not found!
sqlite select one: sql: no rows in result set

results matching ""

    No results matching ""