嘗試使用gorp進行 SELECT 時收到以下錯誤:No table found for type: Post這是我的代碼的樣子: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)}我正在添加這樣的表格:dbMapper.AddTableWithName(Post{}, "posts").SetKeys(true, "Id")
1 回答

絕地無雙
TA貢獻1946條經驗 獲得超4個贊
看起來你并沒有做錯什么。我使用postgres
驅動程序在本地運行了您的示例(您沒有指定您使用的是哪個驅動程序),它運行得很好 - 我猜是這里缺少一些信息。需要確保的事項:
dbMapper.AddTableWithName(Post{}, "posts")
在嘗試使用該表之前檢查是否已調用該表。您引用的錯誤通常在AddTableWithName
未調用時返回。檢查該表是否確實存在于您的數據庫中,并且您正在使用(類似)連接到正確的數據庫
sql.Open("postgres", "user=postgres dbname=test")
實例化 Gorp 時,請確保使用正確的方言:
dbMapper := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
在 Postgres 的情況下這可能與舊版本的 Gorp 有關 - 自從提出這個問題已經幾個月了,現在升級可能會解決這個問題。
除此之外,我認為我們需要更多信息來深入了解這個問題。
- 1 回答
- 0 關注
- 224 瀏覽
添加回答
舉報
0/150
提交
取消