orm now use a filed named `Id` as default auto primary key, you can ignore `orm:"auto"` setting
Showing
3 changed files
with
44 additions
and
18 deletions
| ... | @@ -32,8 +32,26 @@ func registerModel(model interface{}) { | ... | @@ -32,8 +32,26 @@ func registerModel(model interface{}) { |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | if info.fields.pk == nil { | 34 | if info.fields.pk == nil { |
| 35 | fmt.Printf("<orm.RegisterModel> `%s` need a primary key field\n", name) | 35 | outFor: |
| 36 | os.Exit(2) | 36 | for _, fi := range info.fields.fieldsDB { |
| 37 | if fi.name == "Id" { | ||
| 38 | if fi.sf.Tag.Get(defaultStructTagName) == "" { | ||
| 39 | switch fi.addrValue.Elem().Kind() { | ||
| 40 | case reflect.Int, reflect.Int64, reflect.Int32: | ||
| 41 | fi.auto = true | ||
| 42 | fi.pk = true | ||
| 43 | info.fields.pk = fi | ||
| 44 | break outFor | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | if info.fields.pk == nil { | ||
| 51 | fmt.Printf("<orm.RegisterModel> `%s` need a primary key field\n", name) | ||
| 52 | os.Exit(2) | ||
| 53 | } | ||
| 54 | |||
| 37 | } | 55 | } |
| 38 | 56 | ||
| 39 | info.table = table | 57 | info.table = table | ... | ... |
| ... | @@ -89,7 +89,7 @@ type fieldInfo struct { | ... | @@ -89,7 +89,7 @@ type fieldInfo struct { |
| 89 | fullName string | 89 | fullName string |
| 90 | column string | 90 | column string |
| 91 | addrValue reflect.Value | 91 | addrValue reflect.Value |
| 92 | sf *reflect.StructField | 92 | sf reflect.StructField |
| 93 | auto bool | 93 | auto bool |
| 94 | pk bool | 94 | pk bool |
| 95 | null bool | 95 | null bool |
| ... | @@ -244,7 +244,7 @@ checkType: | ... | @@ -244,7 +244,7 @@ checkType: |
| 244 | fi.name = sf.Name | 244 | fi.name = sf.Name |
| 245 | fi.column = getColumnName(fieldType, addrField, sf, tags["column"]) | 245 | fi.column = getColumnName(fieldType, addrField, sf, tags["column"]) |
| 246 | fi.addrValue = addrField | 246 | fi.addrValue = addrField |
| 247 | fi.sf = &sf | 247 | fi.sf = sf |
| 248 | fi.fullName = mi.fullName + "." + sf.Name | 248 | fi.fullName = mi.fullName + "." + sf.Name |
| 249 | 249 | ||
| 250 | fi.null = attrs["null"] | 250 | fi.null = attrs["null"] |
| ... | @@ -358,6 +358,14 @@ checkType: | ... | @@ -358,6 +358,14 @@ checkType: |
| 358 | 358 | ||
| 359 | if fi.auto || fi.pk { | 359 | if fi.auto || fi.pk { |
| 360 | if fi.auto { | 360 | if fi.auto { |
| 361 | |||
| 362 | switch addrField.Elem().Kind() { | ||
| 363 | case reflect.Int, reflect.Int32, reflect.Int64: | ||
| 364 | default: | ||
| 365 | err = fmt.Errorf("auto primary key only support int, int32, int64, but found `%s`", addrField.Elem().Kind()) | ||
| 366 | goto end | ||
| 367 | } | ||
| 368 | |||
| 361 | fi.pk = true | 369 | fi.pk = true |
| 362 | } | 370 | } |
| 363 | fi.null = false | 371 | fi.null = false | ... | ... |
| ... | @@ -13,10 +13,10 @@ import ( | ... | @@ -13,10 +13,10 @@ import ( |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | type Data struct { | 15 | type Data struct { |
| 16 | Id int `orm:"auto"` | 16 | Id int |
| 17 | Boolean bool | 17 | Boolean bool |
| 18 | Char string `orm:"size(50)"` | 18 | Char string `orm:size(50)` |
| 19 | Text string | 19 | Text string `orm:"type(text)"` |
| 20 | Date time.Time `orm:"type(date)"` | 20 | Date time.Time `orm:"type(date)"` |
| 21 | DateTime time.Time | 21 | DateTime time.Time |
| 22 | Byte byte | 22 | Byte byte |
| ... | @@ -37,10 +37,10 @@ type Data struct { | ... | @@ -37,10 +37,10 @@ type Data struct { |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | type DataNull struct { | 39 | type DataNull struct { |
| 40 | Id int `orm:"auto"` | 40 | Id int |
| 41 | Boolean bool `orm:"null"` | 41 | Boolean bool `orm:"null"` |
| 42 | Char string `orm:"size(50);null"` | 42 | Char string `orm:"size(50);null"` |
| 43 | Text string `orm:"null"` | 43 | Text string `orm:"type(text);null"` |
| 44 | Date time.Time `orm:"type(date);null"` | 44 | Date time.Time `orm:"type(date);null"` |
| 45 | DateTime time.Time `orm:"null"` | 45 | DateTime time.Time `orm:"null"` |
| 46 | Byte byte `orm:"null"` | 46 | Byte byte `orm:"null"` |
| ... | @@ -61,7 +61,7 @@ type DataNull struct { | ... | @@ -61,7 +61,7 @@ type DataNull struct { |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | type User struct { | 63 | type User struct { |
| 64 | Id int `orm:"auto"` | 64 | Id int |
| 65 | UserName string `orm:"size(30);unique"` | 65 | UserName string `orm:"size(30);unique"` |
| 66 | Email string `orm:"size(100)"` | 66 | Email string `orm:"size(100)"` |
| 67 | Password string `orm:"size(100)"` | 67 | Password string `orm:"size(100)"` |
| ... | @@ -81,10 +81,10 @@ func NewUser() *User { | ... | @@ -81,10 +81,10 @@ func NewUser() *User { |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | type Profile struct { | 83 | type Profile struct { |
| 84 | Id int `orm:"auto"` | 84 | Id int |
| 85 | Age int16 `` | 85 | Age int16 |
| 86 | Money float64 `` | 86 | Money float64 |
| 87 | User *User `orm:"reverse(one)" json:"-"` | 87 | User *User `orm:"reverse(one)" json:"-"` |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | func (u *Profile) TableName() string { | 90 | func (u *Profile) TableName() string { |
| ... | @@ -97,8 +97,8 @@ func NewProfile() *Profile { | ... | @@ -97,8 +97,8 @@ func NewProfile() *Profile { |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | type Post struct { | 99 | type Post struct { |
| 100 | Id int `orm:"auto"` | 100 | Id int |
| 101 | User *User `orm:"rel(fk)"` // | 101 | User *User `orm:"rel(fk)"` |
| 102 | Title string `orm:"size(60)"` | 102 | Title string `orm:"size(60)"` |
| 103 | Content string `` | 103 | Content string `` |
| 104 | Created time.Time `orm:"auto_now_add"` | 104 | Created time.Time `orm:"auto_now_add"` |
| ... | @@ -112,7 +112,7 @@ func NewPost() *Post { | ... | @@ -112,7 +112,7 @@ func NewPost() *Post { |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | type Tag struct { | 114 | type Tag struct { |
| 115 | Id int `orm:"auto"` | 115 | Id int |
| 116 | Name string `orm:"size(30)"` | 116 | Name string `orm:"size(30)"` |
| 117 | Posts []*Post `orm:"reverse(many)" json:"-"` | 117 | Posts []*Post `orm:"reverse(many)" json:"-"` |
| 118 | } | 118 | } |
| ... | @@ -123,7 +123,7 @@ func NewTag() *Tag { | ... | @@ -123,7 +123,7 @@ func NewTag() *Tag { |
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | type Comment struct { | 125 | type Comment struct { |
| 126 | Id int `orm:"auto"` | 126 | Id int |
| 127 | Post *Post `orm:"rel(fk)"` | 127 | Post *Post `orm:"rel(fk)"` |
| 128 | Content string `` | 128 | Content string `` |
| 129 | Parent *Comment `orm:"null;rel(fk)"` | 129 | Parent *Comment `orm:"null;rel(fk)"` | ... | ... |
-
Please register or sign in to post a comment