jmoiron / sqlx

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

missing destination name ... error will occur if use column name : rec_created and rec_created_by #226

Open hatuan opened 8 years ago

hatuan commented 8 years ago

If I use table like that

type Client struct {
...
    RecCreatedBy                string          `json:"rec_created_by"  db:"rec_created_by"`
    RecCreated                  *time.Time      `json:"rec_created" db:"rec_created"`
...
}

error "missing destination name rec_created_by" will occur

But ok if I change table to that

type Client struct {
...
    RecCreatedBy                string          `json:"rec_created_by"  db:"rec_created_by"`
    RecCreated                  *time.Time      `json:"rec_created" db:"rec_created_at"`
...
}
jmoiron commented 8 years ago

Hi Huatan,

I've tried to create a failing test where the reflect machinery could not find rec_created, but it did not fail. the test is linked above. If you have more information about what failed that can be helpful:

hatuan commented 8 years ago

Here is my code

type Client struct { ClientID string json:"client_id" db:"id" Name string json:"name" db:"name" Version int16 json:"version" db:"version" IsActivated bool json:"is_activated" db:"is_activated" RecCreatedBy string json:"rec_created_by" db:"rec_created_by" RecCreatedByUser User json:"rec_created_by_user" db:"-" RecCreated time.Time json:"rec_created" db:"rec_created" RecModifiedBy string json:"rec_modified_by" db:"rec_modified_by" RecModifiedByUser User json:"rec_modified_by_user" db:"-" RecModified time.Time json:"rec_modified" db:"rec_modified" CultureID string json:"culture_id" db:"culture_id" ... } func (c *Client) Get(id string) error { db, err := sqlx.Connect(settings.Settings.Database.DriverName, settings.Settings.GetDbConn()) if err != nil { log.Fatal(err) } defer db.Close() _client := Client{} err = db.QueryRowx("SELECT id, name, version, is_activated, rec_created_by, rec_created, rec_modified_by, rec_modified, culture_id FROM client WHERE id=$1", id).StructScan(&_client) if err == sql.ErrNoRows { return ErrClientNotFound } else if err != nil { return err } return nil }