Reputation: 7024
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
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:
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.sql.Open("postgres", "user=postgres dbname=test")
dbMapper := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
in the case of PostgresOther than that I think we'll need more information to get to the bottom of this one.
Upvotes: 1