Skip to content

Commit

Permalink
Use tag to set primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 29, 2014
1 parent f56e071 commit 0c63e57
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ If you have an existing database schema, and the primary key field is different

```go
type Animal struct {
AnimalId int64 `primaryKey:"yes"`
AnimalId int64 `gorm:"primary_key:yes"`
Birthday time.Time
Age int64
}
Expand Down
11 changes: 11 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func TestCreate(t *testing.T) {
}
}

func TestCreateWithNoStdPrimaryKey(t *testing.T) {
animal := Animal{Name: "Ferdinand"}
if db.Save(&animal).Error != nil {
t.Errorf("No error should happen when create an record without std primary key")
}

if animal.Counter == 0 {
t.Errorf("No std primary key should be filled value after create")
}
}

func TestAnonymousScanner(t *testing.T) {
user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
db.Save(&user)
Expand Down
2 changes: 1 addition & 1 deletion main_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *DB) print(v ...interface{}) {
}

func (s *DB) log(v ...interface{}) {
if s.logMode == 2 {
if s != nil && s.logMode == 2 {
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
}
}
Expand Down
5 changes: 4 additions & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ func (scope *Scope) Fields() []*Field {
field.IsBlank = isBlank(value)

// Search for primary key tag identifier
field.isPrimaryKey = scope.PrimaryKey() == field.DBName || fieldStruct.Tag.Get("primaryKey") != ""
settings := parseTagSetting(fieldStruct.Tag.Get("gorm"))
if _, ok := settings["PRIMARY_KEY"]; scope.PrimaryKey() == field.DBName || ok {
field.isPrimaryKey = true
}

if field.isPrimaryKey {
scope.primaryKey = field.DBName
Expand Down
2 changes: 1 addition & 1 deletion structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (i *Num) Scan(src interface{}) error {
}

type Animal struct {
Counter int64 `primaryKey:"yes"`
Counter int64 `gorm:"primary_key:yes"`
Name string
From string //test reserved sql keyword as field name
CreatedAt time.Time
Expand Down
3 changes: 2 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func GetPrimaryKey(value interface{}) string {
continue
}

if fieldStruct.Tag.Get("primaryKey") != "" {
settings := parseTagSetting(fieldStruct.Tag.Get("gorm"))
if _, ok := settings["PRIMARY_KEY"]; ok {
return fieldStruct.Name
}
}
Expand Down

0 comments on commit 0c63e57

Please sign in to comment.