2016-03-23 07:18:41 +01:00
|
|
|
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
2016-03-18 12:26:48 +01:00
|
|
|
// {{$tableNameSingular}}Insert inserts a single record.
|
2016-04-13 15:51:58 +02:00
|
|
|
func (o *{{$tableNameSingular}}) Insert() (int, error) {
|
2016-02-23 13:38:24 +01:00
|
|
|
if o == nil {
|
2016-03-23 07:18:41 +01:00
|
|
|
return 0, errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doBeforeCreateHooks(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:38:24 +01:00
|
|
|
var rowID int
|
2016-04-13 15:51:58 +02:00
|
|
|
err := boil.GetDB().QueryRow(`INSERT INTO {{.Table.Name}} ({{insertParamNames .Table.Columns}}) VALUES({{insertParamFlags .Table.Columns}}) RETURNING id`, {{insertParamVariables "o." .Table.Columns}}).Scan(&rowID)
|
2016-02-23 13:38:24 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2016-03-23 07:18:41 +01:00
|
|
|
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{.Table.Name}}: %s", err)
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-13 15:51:58 +02:00
|
|
|
if err := o.doAfterCreateHooks(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:38:24 +01:00
|
|
|
return rowID, nil
|
|
|
|
}
|