vinnylinux
vinnylinux

Reputation: 7024

Problems using Select in gorp

I'm receiving the following error when trying to SELECT with gorp:

No table found for type: Post

Here's what my code looks like:

type Post struct {
    Id         int64  `db:"post_id"`
    CreatedAt  int64  `db:"created_at"`
    UpdatedAt  int64  `db:"updated"`
    Title      string `db:"title"`
}

var list []*Post
_, err := dbMapper.Select(&list, "SELECT * FROM posts")

if (err != nil) {
    fmt.Fprintf(writer, "%s", err)
    return
}

for _, item := range list {
    fmt.Fprintf(writer, "%s\n", item.Title)
}

I'm adding the table like this:

dbMapper.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")

Upvotes: 3

Views: 1269

Answers (1)

Herman Schaaf
Herman Schaaf

Reputation: 48435

It doesn't seem like you're doing anything particularly wrong. I ran your example locally using the postgres driver (you didn't specify which driver you are using), and it worked just fine - my guess is that there's some information missing here. Things to make sure of:

  • Check that dbMapper.AddTableWithName(Post{}, "posts") got called for the table before attempting to use it. The error you refer to is normally returned when AddTableWithName was not called.
  • Check that the table actually exists in your database, and that you're connecting to the correct database with (something like) sql.Open("postgres", "user=postgres dbname=test")
  • When instantiating Gorp, make sure you're using the right dialect: dbMapper := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}} in the case of Postgres
  • This could be related to an older version of Gorp - it's been a couple of months since this question was asked, upgrading now might resolve the issue.

Other than that I think we'll need more information to get to the bottom of this one.

Upvotes: 1

Related Questions