@@ -251,16 +251,32 @@ func buildColumnAndConstraint(ctx sessionctx.Context, offset int,
251
251
return col , cts , nil
252
252
}
253
253
254
- // checkColumnCantHaveDefaultValue checks the column can have value as default or not.
255
- // Now, TEXT/BLOB/JSON can't have not null value as default.
256
- func checkColumnCantHaveDefaultValue (col * table.Column , value interface {}) (err error ) {
254
+ // checkColumnDefaultValue checks the default value of the column.
255
+ // In non-strict SQL mode, if the default value of the column is an empty string, the default value can be ignored.
256
+ // In strict SQL mode, TEXT/BLOB/JSON can't have not null default values.
257
+ func checkColumnDefaultValue (ctx sessionctx.Context , col * table.Column , value interface {}) (bool , interface {}, error ) {
258
+ hasDefaultValue := true
257
259
if value != nil && (col .Tp == mysql .TypeJSON ||
258
260
col .Tp == mysql .TypeTinyBlob || col .Tp == mysql .TypeMediumBlob ||
259
261
col .Tp == mysql .TypeLongBlob || col .Tp == mysql .TypeBlob ) {
260
- // TEXT/BLOB/JSON can't have not null default values.
261
- return errBlobCantHaveDefault .GenByArgs (col .Name .O )
262
+ // In non-strict SQL mode.
263
+ if ! ctx .GetSessionVars ().SQLMode .HasStrictMode () && value == "" {
264
+ if col .Tp == mysql .TypeBlob || col .Tp == mysql .TypeLongBlob {
265
+ // The TEXT/BLOB default value can be ignored.
266
+ hasDefaultValue = false
267
+ }
268
+ // In non-strict SQL mode, if the column type is json and the default value is null, it is initialized to an empty array.
269
+ if col .Tp == mysql .TypeJSON {
270
+ value = `null`
271
+ }
272
+ sc := ctx .GetSessionVars ().StmtCtx
273
+ sc .AppendWarning (errBlobCantHaveDefault .GenByArgs (col .Name .O ))
274
+ return hasDefaultValue , value , nil
275
+ }
276
+ // In strict SQL mode or default value is not an empty string.
277
+ return hasDefaultValue , value , errBlobCantHaveDefault .GenByArgs (col .Name .O )
262
278
}
263
- return nil
279
+ return hasDefaultValue , value , nil
264
280
}
265
281
266
282
// isExplicitTimeStamp is used to check if explicit_defaults_for_timestamp is on or off.
@@ -326,11 +342,10 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
326
342
if err != nil {
327
343
return nil , nil , ErrColumnBadNull .Gen ("invalid default value - %s" , err )
328
344
}
329
- if err = checkColumnCantHaveDefaultValue ( col , value ); err != nil {
345
+ if hasDefaultValue , value , err = checkColumnDefaultValue ( ctx , col , value ); err != nil {
330
346
return nil , nil , errors .Trace (err )
331
347
}
332
348
col .DefaultValue = value
333
- hasDefaultValue = true
334
349
removeOnUpdateNowFlag (col )
335
350
case ast .ColumnOptionOnUpdate :
336
351
// TODO: Support other time functions.
@@ -1462,11 +1477,10 @@ func setDefaultAndComment(ctx sessionctx.Context, col *table.Column, options []*
1462
1477
if err != nil {
1463
1478
return ErrColumnBadNull .Gen ("invalid default value - %s" , err )
1464
1479
}
1465
- if err = checkColumnCantHaveDefaultValue ( col , value ); err != nil {
1480
+ if hasDefaultValue , value , err = checkColumnDefaultValue ( ctx , col , value ); err != nil {
1466
1481
return errors .Trace (err )
1467
1482
}
1468
1483
col .DefaultValue = value
1469
- hasDefaultValue = true
1470
1484
case ast .ColumnOptionComment :
1471
1485
err := setColumnComment (ctx , col , opt )
1472
1486
if err != nil {
0 commit comments