dourunlao1642 2013-09-23 21:30
浏览 47
已采纳

在Gorp中使用Select时出现问题

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
", item.Title)
}

I'm adding the table like this:

dbMapper.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2014-05-25 12:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测