sqlboiler/cmds/templates/insert.tpl

17 lines
572 B
Smarty
Raw Normal View History

2016-02-23 13:38:24 +01:00
{{- $tableName := .TableName -}}
2016-03-01 16:53:56 +01:00
// {{makeGoName $tableName}}Insert inserts a single record.
func {{makeGoName $tableName}}Insert(db boil.DB, o *{{makeGoName $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}} ({{makeGoInsertParamNames .TableData}}) VALUES({{makeGoInsertParamFlags .TableData}}) 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
}