sqlboiler/cmds/templates/insert.tpl

17 lines
624 B
Smarty
Raw Normal View History

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