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

Nested struct without db tag not omitted on insert or update #678

Open
lampi87 opened this issue Jan 16, 2023 · 0 comments
Open

Nested struct without db tag not omitted on insert or update #678

lampi87 opened this issue Jan 16, 2023 · 0 comments

Comments

@lampi87
Copy link

lampi87 commented Jan 16, 2023

Installed version: 4.6.0
DB: mysql

It seems that nested structs without db tag are not omitted on insert or update. Is this a bug or do I need to set a specific db tag to ignore nested structs?

An example, a user struct with a current house set, and a house struct.

type User struct {
	ID             int32          `db:"id,omitempty"`
	Username       string         `db:"username"`
	Password       sql.NullString `db:"password"`
	Email          string         `db:"email"`
	ApiToken       string         `db:"apiToken"`
	CurrentHouseID int32          `db:"currentHouse_id"`

	CurrentHouse house       # Should be ignored for upper/db because no `db:",inline"` or similar set
	Houses       []House
}
type House struct {
	ID           int32         `db:"id,omitempty"`
	Name         string        `db:"name"`
	State        bool          `db:"state"`
	CreateDate   time.Time     `db:"createDate"`
	CreateUserID sql.NullInt32 `db:"createUser_id"`
	EditDate     sql.NullTime  `db:"editDate"`
	EditUserID   sql.NullInt32 `db:"editUser_id"`
}

If I remove nested struct CurrentHouse from User everything works fine. But if I add CurrentHouse update query fails to execute because all fields of House are added to this query, when using sess.SQL().Update("user1").Set(&user).Where("id = ?", id)

Session ID:     00001
Query:          UPDATE `user1` SET `apiToken` = ?, `createDate` = ?, `createUser_id` = ?, `currentHouse_id` = ?, `editDate` = ?, `editUser_id` = ?, `email` = ?, `id` = ?, `name` = ?, `oid` = ?, `password` = ?, `state` = ?, `username` = ? WHERE (id = ?)
Arguments:      ...
Stack:          ...
Error:          Error 1054 (42S22): Unknown column 'createDate' in 'field list'
Time taken:     0.00000s
Context:        context.Background

Struct User does not have fields set like createDate, createUser_id,...

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

1 participant