4cae7af3 by supar

Add attribute DEFAULT '' to the CREAT, ALTER constructors

1 parent e4988b71
...@@ -104,7 +104,11 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string { ...@@ -104,7 +104,11 @@ 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", Q, fi.mi.table, Q, Q, fi.column, Q, typ) 107 return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s %s",
108 Q, fi.mi.table, Q,
109 Q, fi.column, Q,
110 typ, getColumnDefault(fi),
111 )
108 } 112 }
109 113
110 // create database creation string. 114 // create database creation string.
...@@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex ...@@ -155,6 +159,9 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
155 //if fi.initial.String() != "" { 159 //if fi.initial.String() != "" {
156 // column += " DEFAULT " + fi.initial.String() 160 // column += " DEFAULT " + fi.initial.String()
157 //} 161 //}
162
163 // Append attribute DEFAULT
164 column += getColumnDefault(fi)
158 165
159 if fi.unique { 166 if fi.unique {
160 column += " " + "UNIQUE" 167 column += " " + "UNIQUE"
...@@ -239,3 +246,42 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex ...@@ -239,3 +246,42 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
239 246
240 return 247 return
241 } 248 }
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 t = " DEFAULT '%s' "
258
259 // These defaults will be useful if there no config value orm:"default" and NOT NULL is on
260 switch fieldType {
261 case TypeDateField:
262 d = "0000-00-00"
263
264 case TypeDateTimeField:
265 d = "0000-00-00 00:00:00"
266
267 case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
268 TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
269 TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField:
270 TypeDecimalField:
271 d = "0"
272 }
273
274 if fi.colDefault {
275 if !fi.initial.Exist() {
276 v = fmt.Strintf(t, "")
277 } else {
278 v = fmt.Strintf(t, fi.initial.String())
279 }
280 } else {
281 if !fi.null {
282 v = fmt.Strintf(t, d)
283 }
284 }
285
286 return v
287 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!