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

Bug in createColumnMap #115

Closed
efureev opened this issue Jul 30, 2019 · 1 comment
Closed

Bug in createColumnMap #115

efureev opened this issue Jul 30, 2019 · 1 comment

Comments

@efureev
Copy link

efureev commented Jul 30, 2019

type DeepPermission struct {
		IDStr string `json:"id"`
		Sep   string `json:"sep"`
	}

	type Permission struct {
		DeepPermission

		IDStr       string    `json:"id" db:"id"`
		Name        string    `json:"name" db:"name"`
		Description string    `json:"description" db:"description"`
		CreatedAt   time.Time `json:"-"`
	}

	p := &Permission{
		Name:        `app:create`,
		IDStr:       `Create a new App`,
		Description: `Description Create a new App`,
	}

sql, arg, err := r.db.Insert(`rbac_permissions`).
		Returning(goqu.C(`id`)).
		Rows(p).
		ToSQL()
  1. with SetColumnRenameFunction == -
goqu.SetColumnRenameFunction(func(v string) string {
	return "-"
})
...
// sql = "INSERT INTO "rbac_permissions" ("-", "description", "id", "name") VALUES ('0001-01-01T00:00:00Z', 'Description Create a new App', 'Create a new App', 'app:create')"
  1. with SetColumnRenameFunction == ``
goqu.SetColumnRenameFunction(func(v string) string {
	return ""
})
...
// sql =  "INSERT INTO "rbac_permissions" (, "description", "id", "name") VALUES ('0001-01-01T00:00:00Z', 'Description Create a new App', 'Create a new App', 'app:create')"
  1. without SetColumnRenameFunction
// sql =  "INSERT INTO "rbac_permissions" ("createdat", "description", "id", "idstr", "name", "sep") VALUES ('0001-01-01T00:00:00Z', 'Description Create a new App', 'Create a new App', '', 'app:create', '')"

In steps above expects follow sql:
INSERT INTO "rbac_permissions" ("description", "id", "name") VALUES ('Description Create a new App', 'Create a new App', 'app:create')

Or am I wrong?

@doug-martin
Copy link
Owner

The SetColumnRenameFunction will be for any column that doesnt have a db tag. It works just like the json tag where you need to explicitly ignore the field for it not to be used. The SetColumnRename function is used to transform column names that dont have a db tag.

In the first example its working as expected, goqu doesnt try to interpret what you transformed the field name to, instead it uses it as is.

The second example is an interesting one in that you returned an empty string and it generated the (, That should probably be an error when creating the SQL for an empty identifier.

In the third example it is also working as expected, if you want to ignore the CreatedAt column it should be changed to:

type Permission struct {
	DeepPermission

	IDStr       string    `json:"id" db:"id"`
	Name        string    `json:"name" db:"name"`
	Description string    `json:"description" db:"description"`
	CreatedAt   time.Time `json:"-" db:"-"`
}

doug-martin added a commit that referenced this issue Aug 2, 2019
* Return an error when an empty identifier is encountered #115
doug-martin added a commit that referenced this issue Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants