Created
December 30, 2013 19:31
-
-
Save jamesharr/8186831 to your computer and use it in GitHub Desktop.
Table naming bug in gorm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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