From c34c514bbaa474da28e02b1eb61d5a89ac9c2e99 Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 24 Oct 2014 14:15:31 +0400 Subject: [PATCH 1/2] Skip add DEFAULT if the field is in relations (rel or reverse) --- orm/cmd_utils.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 8304da6b..c7910ca2 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -239,3 +239,46 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex return } + +// Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands +func getColumnDefault(fi *fieldInfo) string { + var ( + v, t, d string + ) + + // Skip default attribute if field is in relations + if fi.rel || fi.reverse { + return v + } + + t = " DEFAULT '%s' " + + // These defaults will be useful if there no config value orm:"default" and NOT NULL is on + switch fi.fieldType { + case TypeDateField: + d = "0000-00-00" + + case TypeDateTimeField: + d = "0000-00-00 00:00:00" + + case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, + TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, + TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField, + TypeDecimalField: + d = "0" + } + + if fi.colDefault { + if !fi.initial.Exist() { + v = fmt.Sprintf(t, "") + } else { + v = fmt.Sprintf(t, fi.initial.String()) + } + } else { + if !fi.null { + v = fmt.Sprintf(t, d) + } + } + + return v +} From 6f5162461ed46f2341cd40129495423f158ac25a Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 24 Oct 2014 14:51:35 +0400 Subject: [PATCH 2/2] Add column DEFAULT attribute. Do not add if field is key or in relations. --- orm/cmd_utils.go | 30 +++++++++++++++++++----------- orm/models_info_f.go | 6 ++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index c7910ca2..09f94487 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -104,7 +104,11 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string { typ += " " + "NOT NULL" } - return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ) + return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s", + Q, fi.mi.table, Q, + Q, fi.column, Q, + typ, getColumnDefault(fi), + ) } // create database creation string. @@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex //if fi.initial.String() != "" { // column += " DEFAULT " + fi.initial.String() //} + + // Append attribute DEFAULT + column += getColumnDefault(fi) if fi.unique { column += " " + "UNIQUE" @@ -240,6 +247,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex return } + // Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands func getColumnDefault(fi *fieldInfo) string { var ( @@ -255,19 +263,19 @@ func getColumnDefault(fi *fieldInfo) string { // These defaults will be useful if there no config value orm:"default" and NOT NULL is on switch fi.fieldType { - case TypeDateField: - d = "0000-00-00" - - case TypeDateTimeField: - d = "0000-00-00 00:00:00" - - case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, - TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, + case TypeDateField: + d = "0000-00-00" + + case TypeDateTimeField: + d = "0000-00-00 00:00:00" + + case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, + TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField, TypeDecimalField: - d = "0" + d = "0" } - + if fi.colDefault { if !fi.initial.Exist() { v = fmt.Sprintf(t, "") diff --git a/orm/models_info_f.go b/orm/models_info_f.go index a79ffab2..84a0c024 100644 --- a/orm/models_info_f.go +++ b/orm/models_info_f.go @@ -116,6 +116,7 @@ type fieldInfo struct { null bool index bool unique bool + colDefault bool initial StrTo size int auto_now bool @@ -280,6 +281,11 @@ checkType: fi.pk = attrs["pk"] fi.unique = attrs["unique"] + // Mark object property if there is attribute "default" in the orm configuration + if _, ok := tags["default"]; ok { + fi.colDefault = true + } + switch fieldType { case RelManyToMany, RelReverseMany, RelReverseOne: fi.null = false