From b6446659525ba624b95db8a13c0f4ad4cc0e4818 Mon Sep 17 00:00:00 2001 From: slene Date: Wed, 6 Nov 2013 22:05:10 +0800 Subject: [PATCH] orm set relation column name #259 --- orm/models_test.go | 2 +- orm/models_utils.go | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/orm/models_test.go b/orm/models_test.go index 561d6254..8179421e 100644 --- a/orm/models_test.go +++ b/orm/models_test.go @@ -150,7 +150,7 @@ func NewTag() *Tag { type Comment struct { Id int - Post *Post `orm:"rel(fk)"` + Post *Post `orm:"rel(fk);column(post)"` Content string `orm:"type(text)"` Parent *Comment `orm:"null;rel(fk)"` Created time.Time `orm:"auto_now_add"` diff --git a/orm/models_utils.go b/orm/models_utils.go index 76ee0b6f..38095b7e 100644 --- a/orm/models_utils.go +++ b/orm/models_utils.go @@ -73,13 +73,16 @@ func getTableUnique(val reflect.Value) [][]string { } func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string { - column := strings.ToLower(col) - if column == "" { + col = strings.ToLower(col) + column := col + if col == "" { column = snakeString(sf.Name) } switch ft { case RelForeignKey, RelOneToOne: - column = column + "_id" + if len(col) == 0 { + column = column + "_id" + } case RelManyToMany, RelReverseMany, RelReverseOne: column = sf.Name }