Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to ignore a field, but I need to join to query the field. What should I do? #394

Open
hinet opened this issue Apr 19, 2019 · 3 comments

Comments

@hinet
Copy link

hinet commented Apr 19, 2019

type Article struct {
    Id         int64 `db:"id, primarykey, autoincrement"`
    Title      string
    CategoryName   string `db:"-" json:"category_name"`
    CategoryId int64  `db:"category_id" json:"category_id"`
}

type Category struct{
    Id         int64 `db:"id, primarykey, autoincrement"`
    Name string
}

var list []Article
_,err := Dbm.Select(&list,"select a.*,c.name CategoryName from Article a,Category c where a.category_id = c.id")

I want to ignore a field, but I need to join to query the field. What should I do?

@hinet
Copy link
Author

hinet commented Apr 19, 2019

error:

query article error%!(EXTRA *gorp.NoFieldInTypeError=gorp: no fields [category] in type Article)

@nelsam
Copy link
Member

nelsam commented Apr 19, 2019

For anyone who comes across this: I showed something of a workaround in gitter; I'll see if I can find time to work on a real solution, as well. However, since I rarely have time to work on gorp these days, it's highly encouraged that anyone who has an idea propose a solution in a PR.

@hinet
Copy link
Author

hinet commented Apr 20, 2019

thanks,I'll paste your reply from gitter here for others to refer to..

type Article struct {
     ID         int64 `db:"id, primarykey, autoincrement"` // ID, not Id; read Effective Go for reasoning
    Title      string
    CategoryID `db:"category_id" json:"-"`
    Category Category `db:"-" json:"category"`
}

type Category struct{
    ID         int64 `db:"id, primarykey, autoincrement"`
    Name string
}

// Note: *never* use 'select *' in code.  It makes your code less forward-compatible, which causes
// a huge mess of issues when you start needing to do rolling deploys.  Among other things.
rows, err := Dbm.Query("SELECT a.id, a.title, a.category_id, c.id, c.name FROM Article AS a INNER JOIN Category AS c ON a.category_id = c.id")
if err != nil {
    // handle err
}
for rows.Next() {
    var (
        article Article
        cat Category
    )
    rows.Scan(&a.ID, &a.Title, &a.CategoryID, &c.ID, &c.Name)
    // do what you need to with the article and category
}
if err := rows.Err(); err != nil {
    // handle err
}

wingsofovnia pushed a commit to wingsofovnia/gorp that referenced this issue Jun 17, 2019
wingsofovnia added a commit to wingsofovnia/gorp that referenced this issue Jun 17, 2019
wingsofovnia added a commit to wingsofovnia/gorp that referenced this issue Jun 17, 2019
wingsofovnia added a commit to wingsofovnia/gorp that referenced this issue Jun 17, 2019
wingsofovnia added a commit to wingsofovnia/gorp that referenced this issue Jun 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants