e11a27f1 by astaxie

Merge pull request #888 from astaxie/revert-887-add-column-default-attribute

Revert "Add column default attribute"
2 parents 6c924903 90caeb4c
...@@ -104,11 +104,7 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string { ...@@ -104,11 +104,7 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string {
104 typ += " " + "NOT NULL" 104 typ += " " + "NOT NULL"
105 } 105 }
106 106
107 return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s", 107 return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ)
108 Q, fi.mi.table, Q,
109 Q, fi.column, Q,
110 typ, getColumnDefault(fi),
111 )
112 } 108 }
113 109
114 // create database creation string. 110 // create database creation string.
...@@ -159,9 +155,6 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex ...@@ -159,9 +155,6 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
159 //if fi.initial.String() != "" { 155 //if fi.initial.String() != "" {
160 // column += " DEFAULT " + fi.initial.String() 156 // column += " DEFAULT " + fi.initial.String()
161 //} 157 //}
162
163 // Append attribute DEFAULT
164 column += getColumnDefault(fi)
165 158
166 if fi.unique { 159 if fi.unique {
167 column += " " + "UNIQUE" 160 column += " " + "UNIQUE"
...@@ -246,47 +239,3 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex ...@@ -246,47 +239,3 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
246 239
247 return 240 return
248 } 241 }
249
250
251 // Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
252 func getColumnDefault(fi *fieldInfo) string {
253 var (
254 v, t, d string
255 )
256
257 // Skip default attribute if field is in relations
258 if fi.rel || fi.reverse {
259 return v
260 }
261
262 t = " DEFAULT '%s' "
263
264 // These defaults will be useful if there no config value orm:"default" and NOT NULL is on
265 switch fi.fieldType {
266 case TypeDateField:
267 d = "0000-00-00"
268
269 case TypeDateTimeField:
270 d = "0000-00-00 00:00:00"
271
272 case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
273 TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
274 TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
275 TypeDecimalField:
276 d = "0"
277 }
278
279 if fi.colDefault {
280 if !fi.initial.Exist() {
281 v = fmt.Sprintf(t, "")
282 } else {
283 v = fmt.Sprintf(t, fi.initial.String())
284 }
285 } else {
286 if !fi.null {
287 v = fmt.Sprintf(t, d)
288 }
289 }
290
291 return v
292 }
......
...@@ -116,7 +116,6 @@ type fieldInfo struct { ...@@ -116,7 +116,6 @@ type fieldInfo struct {
116 null bool 116 null bool
117 index bool 117 index bool
118 unique bool 118 unique bool
119 colDefault bool
120 initial StrTo 119 initial StrTo
121 size int 120 size int
122 auto_now bool 121 auto_now bool
...@@ -281,11 +280,6 @@ checkType: ...@@ -281,11 +280,6 @@ checkType:
281 fi.pk = attrs["pk"] 280 fi.pk = attrs["pk"]
282 fi.unique = attrs["unique"] 281 fi.unique = attrs["unique"]
283 282
284 // Mark object property if there is attribute "default" in the orm configuration
285 if _, ok := tags["default"]; ok {
286 fi.colDefault = true
287 }
288
289 switch fieldType { 283 switch fieldType {
290 case RelManyToMany, RelReverseMany, RelReverseOne: 284 case RelManyToMany, RelReverseMany, RelReverseOne:
291 fi.null = false 285 fi.null = false
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!