From e5904443d92a54baf19d87e7327db3c81dfaabcb Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 24 Nov 2013 19:29:48 +0800 Subject: [PATCH] fix #302 --- orm/models_boot.go | 4 +--- orm/models_test.go | 12 +++++++++++- orm/orm_test.go | 2 ++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/orm/models_boot.go b/orm/models_boot.go index 32e143b7..3274b187 100644 --- a/orm/models_boot.go +++ b/orm/models_boot.go @@ -106,9 +106,7 @@ func bootStrap() { msg := fmt.Sprintf("field `%s` wrong rel_through value `%s`", fi.fullName, fi.relThrough) if i := strings.LastIndex(fi.relThrough, "."); i != -1 && len(fi.relThrough) > (i+1) { pn := fi.relThrough[:i] - mn := fi.relThrough[i+1:] - tn := snakeString(mn) - rmi, ok := modelCache.get(tn) + rmi, ok := modelCache.getByFN(fi.relThrough) if ok == false || pn != rmi.pkg { err = errors.New(msg + " cannot find table") goto end diff --git a/orm/models_test.go b/orm/models_test.go index 8179421e..18ee0a59 100644 --- a/orm/models_test.go +++ b/orm/models_test.go @@ -122,7 +122,7 @@ type Post struct { Content string `orm:"type(text)"` Created time.Time `orm:"auto_now_add"` Updated time.Time `orm:"auto_now"` - Tags []*Tag `orm:"rel(m2m)"` + Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.PostTags)"` } func (u *Post) TableIndex() [][]string { @@ -148,6 +148,16 @@ func NewTag() *Tag { return obj } +type PostTags struct { + Id int + Post *Post `orm:"rel(fk)"` + Tag *Tag `orm:"rel(fk)"` +} + +func (m *PostTags) TableName() string { + return "prefix_post_tags" +} + type Comment struct { Id int Post *Post `orm:"rel(fk);column(post)"` diff --git a/orm/orm_test.go b/orm/orm_test.go index 8b81ea8b..156fe754 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -146,6 +146,7 @@ func TestSyncDb(t *testing.T) { RegisterModel(new(Tag)) RegisterModel(new(Comment)) RegisterModel(new(UserBig)) + RegisterModel(new(PostTags)) err := RunSyncdb("default", true, false) throwFail(t, err) @@ -161,6 +162,7 @@ func TestRegisterModels(t *testing.T) { RegisterModel(new(Tag)) RegisterModel(new(Comment)) RegisterModel(new(UserBig)) + RegisterModel(new(PostTags)) BootStrap()