diff --git a/orm/models_boot.go b/orm/models_boot.go index 32e143b7b3c6c61efae1eb8382a4a42b34a4bf7e..3274b187db8e227d73b71a97c369499574fd7771 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 8179421e14bbd68d51f7684cb2f1fac634fa0027..18ee0a596af16b7f5e5ed5b643444c3715204cfb 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 8b81ea8b84c27d468d31d6e723a964a143c7e778..156fe754b54971e40ddfbbd95162d6423bea795d 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()