upper / db

Data Access Layer (DAL) for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
https://upper.io/
MIT License
3.54k stars 235 forks source link

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

Open lampi87 opened 1 year ago

lampi87 commented 1 year ago

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