Categories
Development

How to cause Buffalo transaction middleware to commit?

In trying to use the buffalo-pop/pop/popmw Transaction middleware, I am not having success writing to the database. No errors are returned, and the debug output shows the SQL statements, but the updates and inserts are not committed.

The handler looks like:


func MyHandler(c buffalo.Context) error {
    tx, ok := c.Value("tx").(*pop.Connection)
    if !ok {
        return errors.New("no transaction found")
    }

    f := models.File{
        Name: "file.txt",
    }
    if err := tx.Create(&f); err != nil {
        return err
    }

    return nil
}

app.go:

func App() *buffalo.App {
...
        app.GET("/myhandler", MyHandler)
        app.Use(popmw.Transaction(models.DB))
...
}

If I use DB, _ := pop.Connect("development") for my connection, it works correctly. I also observed that the autoincrement value on the table changes each time this handler is hit.

Leave a Reply

Your email address will not be published. Required fields are marked *