Skip to content

Instantly share code, notes, and snippets.

@jamesharr
Created December 30, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesharr/8186831 to your computer and use it in GitHub Desktop.
Save jamesharr/8186831 to your computer and use it in GitHub Desktop.
Table naming bug in gorm
package main
import "github.com/jinzhu/gorm"
import (
_ "github.com/lib/pq"
"fmt"
)
type Foo struct {
Id int64
}
func (Foo) TableName() string {
return "other_table_name"
}
type Bar struct {
Id int64
FooId int64
}
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
must(err)
db.LogMode(true)
// Set up test
must(db.Exec("DROP TABLE IF EXISTS other_table_name").Error)
must(db.CreateTable(Foo{}).Error)
var arr []Foo
err = db.Where("id > ?", 0).Find(&arr).Error
fmt.Println(err) // pq: relation "foos" does not exist
err = db.Model(&Bar{0,0}).Related(&arr).Error
fmt.Println(err) // pq: relation "foos" does not exist
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment