mirror of https://github.com/go-gorm/gorm.git
yay, support primary key
This commit is contained in:
parent
3c68efc94a
commit
a7f62c24b0
10
model.go
10
model.go
|
@ -23,6 +23,10 @@ func (s *Orm) toModel(value interface{}) *Model {
|
||||||
return &Model{Data: value, driver: s.driver}
|
return &Model{Data: value, driver: s.driver}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Model) PrimaryKey() string {
|
||||||
|
return "Id"
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Model) Fields() (fields []Field) {
|
func (m *Model) Fields() (fields []Field) {
|
||||||
typ := reflect.TypeOf(m.Data).Elem()
|
typ := reflect.TypeOf(m.Data).Elem()
|
||||||
|
|
||||||
|
@ -33,7 +37,11 @@ func (m *Model) Fields() (fields []Field) {
|
||||||
field.Name = p.Name
|
field.Name = p.Name
|
||||||
field.DbName = toSnake(p.Name)
|
field.DbName = toSnake(p.Name)
|
||||||
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
|
field.Value = reflect.ValueOf(m.Data).Elem().FieldByName(p.Name).Interface()
|
||||||
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
if m.PrimaryKey() == p.Name {
|
||||||
|
field.SqlType = getPrimaryKeySqlType(m.driver, field.Value, 0)
|
||||||
|
} else {
|
||||||
|
field.SqlType = getSqlType(m.driver, field.Value, 0)
|
||||||
|
}
|
||||||
fields = append(fields, field)
|
fields = append(fields, field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
orm.go
2
orm.go
|
@ -28,7 +28,7 @@ type Orm struct {
|
||||||
func (s *Orm) setModel(model interface{}) (err error) {
|
func (s *Orm) setModel(model interface{}) (err error) {
|
||||||
s.Model = s.toModel(model)
|
s.Model = s.toModel(model)
|
||||||
s.TableName = s.Model.TableName()
|
s.TableName = s.Model.TableName()
|
||||||
s.PrimaryKey = "id"
|
s.PrimaryKey = s.Model.PrimaryKey()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
sql_type.go
25
sql_type.go
|
@ -5,6 +5,27 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getPrimaryKeySqlType(adaptor string, column interface{}, size int) string {
|
||||||
|
switch adaptor {
|
||||||
|
case "mysql":
|
||||||
|
suffix_str := " NOT NULL AUTO_INCREMENT PRIMARY KEY"
|
||||||
|
switch column.(type) {
|
||||||
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
||||||
|
return "int" + suffix_str
|
||||||
|
case int64, uint64:
|
||||||
|
return "bigint" + suffix_str
|
||||||
|
}
|
||||||
|
case "postgres":
|
||||||
|
switch column.(type) {
|
||||||
|
case int, int8, int16, int32, uint, uint8, uint16, uint32:
|
||||||
|
return "serial"
|
||||||
|
case int64, uint64:
|
||||||
|
return "bigserial"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic("unsupported sql adaptor, please submit an issue in github")
|
||||||
|
}
|
||||||
|
|
||||||
func getSqlType(adaptor string, column interface{}, size int) string {
|
func getSqlType(adaptor string, column interface{}, size int) string {
|
||||||
switch adaptor {
|
switch adaptor {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
|
@ -55,7 +76,7 @@ func getSqlType(adaptor string, column interface{}, size int) string {
|
||||||
default:
|
default:
|
||||||
panic("invalid sql type")
|
panic("invalid sql type")
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
panic("unsupported sql adaptor, please submit an issue in github")
|
||||||
}
|
}
|
||||||
|
|
||||||
panic("unsupported sql adaptor, please submit an issue in github")
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue