sqlboiler/cmds/templates/insert.tpl
Patrick O'brien b5409c7332 Fixed assign defaultRun bug, fixed "all" order
* Organized files better
* More comments
2016-02-29 19:39:49 +10:00

17 lines
582 B
Smarty

{{- $tableName := .TableName -}}
// {{makeGoColName $tableName}}Insert inserts a single record.
func {{makeGoColName $tableName}}Insert(db *sqlx.DB, o *{{makeGoColName $tableName}}) (int, error) {
if o == nil {
return 0, errors.New("model: no {{$tableName}} provided for insertion")
}
var rowID int
err := db.QueryRow(`INSERT INTO {{$tableName}} ({{makeGoInsertParamNames .TableData}}) VALUES({{makeGoInsertParamFlags .TableData}}) RETURNING id`)
if err != nil {
return 0, fmt.Errorf("model: unable to insert {{$tableName}}: %s", err)
}
return rowID, nil
}