6f516246 by supar

Add column DEFAULT attribute. Do not add if field is key or in

relations.
1 parent c34c514b
...@@ -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"
...@@ -240,6 +247,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex ...@@ -240,6 +247,7 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
240 return 247 return
241 } 248 }
242 249
250
243 // Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands 251 // Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands
244 func getColumnDefault(fi *fieldInfo) string { 252 func getColumnDefault(fi *fieldInfo) string {
245 var ( 253 var (
...@@ -255,19 +263,19 @@ func getColumnDefault(fi *fieldInfo) string { ...@@ -255,19 +263,19 @@ func getColumnDefault(fi *fieldInfo) string {
255 263
256 // These defaults will be useful if there no config value orm:"default" and NOT NULL is on 264 // These defaults will be useful if there no config value orm:"default" and NOT NULL is on
257 switch fi.fieldType { 265 switch fi.fieldType {
258 case TypeDateField: 266 case TypeDateField:
259 d = "0000-00-00" 267 d = "0000-00-00"
260 268
261 case TypeDateTimeField: 269 case TypeDateTimeField:
262 d = "0000-00-00 00:00:00" 270 d = "0000-00-00 00:00:00"
263 271
264 case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, 272 case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField,
265 TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, 273 TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField,
266 TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField, 274 TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField,
267 TypeDecimalField: 275 TypeDecimalField:
268 d = "0" 276 d = "0"
269 } 277 }
270 278
271 if fi.colDefault { 279 if fi.colDefault {
272 if !fi.initial.Exist() { 280 if !fi.initial.Exist() {
273 v = fmt.Sprintf(t, "") 281 v = fmt.Sprintf(t, "")
......
...@@ -116,6 +116,7 @@ type fieldInfo struct { ...@@ -116,6 +116,7 @@ type fieldInfo struct {
116 null bool 116 null bool
117 index bool 117 index bool
118 unique bool 118 unique bool
119 colDefault bool
119 initial StrTo 120 initial StrTo
120 size int 121 size int
121 auto_now bool 122 auto_now bool
...@@ -280,6 +281,11 @@ checkType: ...@@ -280,6 +281,11 @@ checkType:
280 fi.pk = attrs["pk"] 281 fi.pk = attrs["pk"]
281 fi.unique = attrs["unique"] 282 fi.unique = attrs["unique"]
282 283
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
283 switch fieldType { 289 switch fieldType {
284 case RelManyToMany, RelReverseMany, RelReverseOne: 290 case RelManyToMany, RelReverseMany, RelReverseOne:
285 fi.null = false 291 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!