Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Generated Column Support #52

Open
daniel-reed opened this issue Oct 19, 2017 · 2 comments
Open

Auto Generated Column Support #52

daniel-reed opened this issue Oct 19, 2017 · 2 comments

Comments

@daniel-reed
Copy link
Member

Add IsAutoGenerated to Column

@xserve98
Copy link

xserve98 commented Oct 30, 2017

func Insert(db {{$rootPkg}}.DB, r *Row) error {
	const sqlstr = `INSERT INTO {{ $table }} (
			{{$insertCols}}
		) VALUES (
			{{ template "values" $numNonPKs }}
		) RETURNING id;`
 

	err := db.QueryRow(sqlstr, {{join  ((.Table.Columns.Names.Except .Table.PrimaryKeys.Names).Sprintf "r.%s") ", "}}).Scan( {{join  (.Table.PrimaryKeys.Names.Sprintf "&r.%s") ", "}})
	return errors.Wrap(err, "insert {{.Table.Name}}")
}

Is there a better solution?
id SERIAL PRIMARY KEY

@xserve98
Copy link

xserve98 commented Oct 30, 2017

// Insert inserts the row into the database.
func Insert(db models.DB, r *Row) error {
		const sqlstr = `INSERT INTO sys_user (
			username, password, salt, avatar, created_at, updated_at
		) VALUES (
			$1, $2, $3, $4, $5, $6
		) RETURNING id;`

	err := db.QueryRow(sqlstr, r.Username, r.Password, r.Salt, r.Avatar, r.CreatedAt,r.UpdatedAt).Scan(&r.ID)
	return errors.Wrap(err, "insert SysUser")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants