orm set relation column name #259
Showing
2 changed files
with
6 additions
and
3 deletions
| ... | @@ -150,7 +150,7 @@ func NewTag() *Tag { | ... | @@ -150,7 +150,7 @@ func NewTag() *Tag { |
| 150 | 150 | ||
| 151 | type Comment struct { | 151 | type Comment struct { |
| 152 | Id int | 152 | Id int |
| 153 | Post *Post `orm:"rel(fk)"` | 153 | Post *Post `orm:"rel(fk);column(post)"` |
| 154 | Content string `orm:"type(text)"` | 154 | Content string `orm:"type(text)"` |
| 155 | Parent *Comment `orm:"null;rel(fk)"` | 155 | Parent *Comment `orm:"null;rel(fk)"` |
| 156 | Created time.Time `orm:"auto_now_add"` | 156 | Created time.Time `orm:"auto_now_add"` | ... | ... |
| ... | @@ -73,13 +73,16 @@ func getTableUnique(val reflect.Value) [][]string { | ... | @@ -73,13 +73,16 @@ func getTableUnique(val reflect.Value) [][]string { |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string { | 75 | func getColumnName(ft int, addrField reflect.Value, sf reflect.StructField, col string) string { |
| 76 | column := strings.ToLower(col) | 76 | col = strings.ToLower(col) |
| 77 | if column == "" { | 77 | column := col |
| 78 | if col == "" { | ||
| 78 | column = snakeString(sf.Name) | 79 | column = snakeString(sf.Name) |
| 79 | } | 80 | } |
| 80 | switch ft { | 81 | switch ft { |
| 81 | case RelForeignKey, RelOneToOne: | 82 | case RelForeignKey, RelOneToOne: |
| 83 | if len(col) == 0 { | ||
| 82 | column = column + "_id" | 84 | column = column + "_id" |
| 85 | } | ||
| 83 | case RelManyToMany, RelReverseMany, RelReverseOne: | 86 | case RelManyToMany, RelReverseMany, RelReverseOne: |
| 84 | column = sf.Name | 87 | column = sf.Name |
| 85 | } | 88 | } | ... | ... |
-
Please register or sign in to post a comment