From c34c514bbaa474da28e02b1eb61d5a89ac9c2e99 Mon Sep 17 00:00:00 2001 From: supar Date: Fri, 24 Oct 2014 14:15:31 +0400 Subject: [PATCH] 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 +}