jmoiron / sqlx

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

NamedExec, Nested Structs, and Overlapping names #507

Open ldelossa opened 5 years ago

ldelossa commented 5 years ago

Hey guys,

I have a question. Let's say I have the structs

type Location struct {
    City         string  `json:"city" db:"city"`
    State        string  `json:"state" db:"state"`
    Zipcode      string  `json:"zipcode" db:"zipcode"`
    Latitude     float64 `json:"latitude" db:"latitude"`
    Longitude    float64 `json:"longitude" db:"longitude"`
    Neighborhood string  `json:"neighborhood" db:"neighborhood"`
        Notes                string
}

// Profile is a retrieved profile
type Profile struct {
    ID        string         `json:"id" db:"id"`
    AccountID string         `json:"account_id" db:"account_id"`
    Images    pq.StringArray `json:"images" db:"images"`
    Name      string         `validate:"required" json:"name" db:"name"`
    Gender   GenderType `json:"gender" db:"gender"`
    Birthday string     `validate:"required" json:"birthday" db:"birthday"`
    PreferenceSex    PreferenceSexType `json:"preference_sex" db:"preference_sex"`
    PreferenceMinAge int               `validate:"required,gte=18,lte=100" json:"preference_min_age" db:"preference_min_age"`
    PreferenceMaxAge int               `validate:"required,gte=18,lte=100" json:"preference_max_age" db:"preference_max_age"`
    Location         `json:"location"`
    CreatedAt        string `json:"created_at" db:"created_at"`
    UpdatedAt        string `json:"updated_at" db:"updated_at"`
         Notes                string
}

As you can see both structs have "Notes" field. I then have a NamedExec query like this:

    CreateProfileQuery            = `INSERT INTO profile (id, account_id, images, name, gender, birthday, preference_sex, preference_min_age, preference_max_age, city, state, zipcode, latitude, longitude, neighborhood, created_at, updated_at, notes) VALUES (:id, :account_id, :images, :name, :gender, :birthday, :preference_sex, :preference_min_age, :preference_max_age, :city, :state, :zipcode, :latitude, :longitude, :neighborhood, :created_at, :updated_at, :notes);`

Is there a way to do something like:

    CreateProfileQuery            = `INSERT INTO profile (id, account_id, images, name, gender, birthday, preference_sex, preference_min_age, preference_max_age, city, state, zipcode, latitude, longitude, neighborhood, created_at, updated_at, notes, location_notes) VALUES (:id, :account_id, :images, :name, :gender, :birthday, :preference_sex, :preference_min_age, :preference_max_age, :city, :state, :zipcode, :latitude, :longitude, :neighborhood, :created_at, :updated_at, :notes, :location.notes);`

So that both "Notes" field get into the database in separate columns?

vitvrn commented 5 years ago

You should add tag:

Location db:"location"

And if you use Oracle (with goracle.v2 driver), which thinks of location.notes as of object.field (unless it is quoted but in this case sqlx.NamedExec won't work) then you should use Named (to get args from your nested structs) and Rebind (to rename :location.notes to :argN) instead of NamedExec.