dialect/sql/schema: do not set timestamp to nullable columns on MySQLv8.* (#1547)

Fixed #1541
This commit is contained in:
Ariel Mashraki
2021-05-08 21:29:35 +03:00
committed by GitHub
parent 0bfaa11e2f
commit 37d4b9e2cb
2 changed files with 12 additions and 6 deletions

View File

@@ -254,9 +254,12 @@ func (d *MySQL) cType(c *Column) (t string) {
t = c.scanTypeOr("double")
case field.TypeTime:
t = c.scanTypeOr("timestamp")
// In MySQL, timestamp columns are `NOT NULL` by default, and assigning NULL
// assigns the current_timestamp(). We avoid this if not set otherwise.
c.Nullable = c.Attr == ""
// In MySQL < v8.0.2, the TIMESTAMP column has both `DEFAULT CURRENT_TIMESTAMP` and
// `ON UPDATE CURRENT_TIMESTAMP` if neither is specified explicitly. this behavior is
// suppressed if the column is defined with a `DEFAULT` clause or with the `NULL` attribute.
if compareVersions(d.version, "8.0.2") == -1 && c.Default == nil {
c.Nullable = c.Attr == ""
}
case field.TypeEnum:
values := make([]string, len(c.Enums))
for i, e := range c.Enums {