@@ -12,6 +12,7 @@ const primaryKeyPrefix = "primary"
1212const uniqueKeyPrefix = "unique"
1313const keyPrefix = "index"
1414const nullPrefix = "null"
15+ const notNullPrefix = "not null"
1516const autoIncrementPrefix = "auto_increment"
1617const createdAtColumn = "created_at"
1718const updatedAtColumn = "updated_at"
@@ -143,7 +144,6 @@ func generateColumnStrings(dbColums []dBColumn) []string {
143144 } else {
144145 words = append (words , "not null" )
145146 }
146-
147147 //add default
148148 if v .AutoIncrement {
149149 words = append (words , "auto_increment" )
@@ -274,14 +274,16 @@ func getMigrateColumns(table *queryTable) []dBColumn {
274274 }
275275
276276 if column .Name == createdAtColumn {
277- column .Type = "timestamp"
277+ column .Null = false
278+ column .Type = "datetime"
278279 column .Default = "CURRENT_TIMESTAMP"
279280 } else if column .Name == updatedAtColumn {
280- column .Type = "timestamp"
281+ column .Null = false
282+ column .Type = "datetime"
281283 column .Default = "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
282284 } else if column .Name == deletedAtColumn {
283285 column .Null = true
284- column .Type = "timestamp "
286+ column .Type = "datetime "
285287 column .Default = "Null"
286288 }
287289
@@ -297,16 +299,24 @@ func getMigrateColumns(table *queryTable) []dBColumn {
297299 }
298300 }
299301 }
302+ if strings .ToLower (column .Default ) == "null" {
303+ column .Null = true
304+ }
300305
301306 if ormTags [0 ] != "" {
302307 overideColumn := dBColumn {}
303308
309+ overrideNull := false
304310 for k , v := range ormTags {
305311 if k == 0 {
306312 continue
307313 }
308314 if v == nullPrefix {
315+ overrideNull = true
309316 overideColumn .Null = true
317+ } else if v == notNullPrefix {
318+ overrideNull = true
319+ overideColumn .Null = false
310320 } else if v == autoIncrementPrefix {
311321 overideColumn .AutoIncrement = true
312322 } else if strings .HasPrefix (v , primaryKeyPrefix ) {
@@ -328,7 +338,9 @@ func getMigrateColumns(table *queryTable) []dBColumn {
328338 }
329339 }
330340
331- column .Null = overideColumn .Null
341+ if overrideNull {
342+ column .Null = overideColumn .Null
343+ }
332344 column .AutoIncrement = overideColumn .AutoIncrement
333345 column .Primary = overideColumn .Primary
334346 if overideColumn .Type != "" {
@@ -358,9 +370,11 @@ func getMigrateColumns(table *queryTable) []dBColumn {
358370
359371func getTypeAndDefault (val reflect.Value ) (string , string ) {
360372 var types , defaults string
373+ typ := val .Type ()
361374 kind := val .Kind ()
362375 if kind == reflect .Ptr {
363- kind = val .Elem ().Kind ()
376+ kind = val .Type ().Elem ().Kind ()
377+ typ = typ .Elem ()
364378 }
365379 switch kind {
366380 case reflect .Bool , reflect .Int8 :
@@ -397,11 +411,21 @@ func getTypeAndDefault(val reflect.Value) (string, string) {
397411 types = "varchar(255)"
398412 default :
399413 if _ , ok := val .Interface ().(* time.Time ); ok {
400- types = "timestamp "
414+ types = "datetime "
401415 } else if _ , ok := val .Interface ().(time.Time ); ok {
402- types = "timestamp "
416+ types = "datetime "
403417 } else {
404- types = "varchar(255)"
418+ realVal := reflect .New (typ )
419+ if _ , ok := realVal .Elem ().Field (0 ).Interface ().(* time.Time ); ok {
420+ types = "datetime"
421+ } else if _ , ok := realVal .Elem ().Field (0 ).Interface ().(time.Time ); ok {
422+ types = "datetime"
423+ } else {
424+ types = "varchar(255)"
425+ }
426+ }
427+ if types == "datetime" {
428+ defaults = "current_timestamp"
405429 }
406430 }
407431 return types , defaults
0 commit comments