Skip add DEFAULT if the field is in relations (rel or reverse)
Showing
1 changed file
with
43 additions
and
0 deletions
| ... | @@ -239,3 +239,46 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex | ... | @@ -239,3 +239,46 @@ func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex |
| 239 | 239 | ||
| 240 | return | 240 | return |
| 241 | } | 241 | } |
| 242 | |||
| 243 | // Get string value for the attribute "DEFAULT" for the CREATE, ALTER commands | ||
| 244 | func getColumnDefault(fi *fieldInfo) string { | ||
| 245 | var ( | ||
| 246 | v, t, d string | ||
| 247 | ) | ||
| 248 | |||
| 249 | // Skip default attribute if field is in relations | ||
| 250 | if fi.rel || fi.reverse { | ||
| 251 | return v | ||
| 252 | } | ||
| 253 | |||
| 254 | t = " DEFAULT '%s' " | ||
| 255 | |||
| 256 | // These defaults will be useful if there no config value orm:"default" and NOT NULL is on | ||
| 257 | switch fi.fieldType { | ||
| 258 | case TypeDateField: | ||
| 259 | d = "0000-00-00" | ||
| 260 | |||
| 261 | case TypeDateTimeField: | ||
| 262 | d = "0000-00-00 00:00:00" | ||
| 263 | |||
| 264 | case TypeBooleanField, TypeBitField, TypeSmallIntegerField, TypeIntegerField, | ||
| 265 | TypeBigIntegerField, TypePositiveBitField, TypePositiveSmallIntegerField, | ||
| 266 | TypePositiveIntegerField, TypePositiveBigIntegerField, TypeFloatField, | ||
| 267 | TypeDecimalField: | ||
| 268 | d = "0" | ||
| 269 | } | ||
| 270 | |||
| 271 | if fi.colDefault { | ||
| 272 | if !fi.initial.Exist() { | ||
| 273 | v = fmt.Sprintf(t, "") | ||
| 274 | } else { | ||
| 275 | v = fmt.Sprintf(t, fi.initial.String()) | ||
| 276 | } | ||
| 277 | } else { | ||
| 278 | if !fi.null { | ||
| 279 | v = fmt.Sprintf(t, d) | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | return v | ||
| 284 | } | ... | ... |
-
Please register or sign in to post a comment