db icon indicating copy to clipboard operation
db copied to clipboard

Nested struct without db tag not omitted on insert or update

Open lampi87 opened this issue 3 years ago • 0 comments

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,...

lampi87 avatar Jan 16 '23 11:01 lampi87