jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.32k stars 1.09k forks source link

missing destination name scene_type in *[]*model.Resource #939

Closed linxiang4200 closed 6 days ago

linxiang4200 commented 3 months ago

Issue description

I have a table named resource and have defined a struct for it. However, I now need to add a new field to the table, but when this field is not yet added to the struct definition in the code, running the code results in an error: "missing destination name scene_type in []model.Resource." ,even though I didn't use this field . How can I avoid this error? Since database changes and code changes are not deployed simultaneously, this could cause issues when the service goes live.

Example code

type Resource struct {
    Id            int          `db:"id" json:"id"`
    CompanyId     int          `db:"company_id" json:"companyId"`
    Pid           int          `db:"pid" json:"pid"`
    CreatorId     int          `db:"creator_id" json:"creatorId"`
    Size          int64        `db:"size" json:"size"`
    Width         int          `db:"width" json:"width"`
    Height        int          `db:"height" json:"height"`
    IsFolder      bool         `db:"is_folder" json:"isFolder"`
    Trashed       bool         `db:"trashed" json:"trashed"`
    Type          ResourceType `db:"type" json:"type"`
    Filename      string       `db:"filename" json:"filename"`
    Extension     string       `db:"extension" json:"extension"`
    Thumbnail     string       `db:"thumbnail" json:"thumbnail"`
    // SceneType     int          `db:"scene_type" json:"sceneType"`   //if I not add this it will be error,even though I didn't use this field 

}

Error log

missing destination name scene_type in *[]*model.Resource
AdenierOsorto commented 6 days ago

Hi, this is the solution

type Person struct { Name string }

var p Person // this will NOT return an error, even though place columns have no destination udb := db.Unsafe() err = udb.Get(&p, "SELECT * FROM person, place LIMIT 1;")

linxiang4200 commented 6 days ago

I have a table named resource and have defined a struct for it. However, I now need to add a new field to the table, but when this field is not yet added to the struct definition in the code, running the code results in an error: "missing destination name scene_type in []model.Resource." ,even though I didn't use this field . How can I avoid this error? Since database changes and code changes are not deployed simultaneously, this could cause issues when the service goes live.

Thanks!