sqlboiler/cmds/templates/insert.tpl

17 lines
549 B
Smarty
Raw Normal View History

{{- $tableName := .Table -}}
// {{titleCase $tableName}}Insert inserts a single record.
func {{titleCase $tableName}}Insert(db boil.DB, o *{{titleCase $tableName}}) (int, error) {
2016-02-23 13:38:24 +01:00
if o == nil {
return 0, errors.New("model: no {{$tableName}} provided for insertion")
2016-02-23 13:38:24 +01:00
}
var rowID int
err := db.QueryRow(`INSERT INTO {{$tableName}} ({{insertParamNames .Columns}}) VALUES({{insertParamFlags .Columns}}) RETURNING id`)
2016-02-23 13:38:24 +01:00
if err != nil {
return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
2016-02-23 13:38:24 +01:00
}
return rowID, nil
}