@@ -326,7 +326,7 @@ func (b *Builder) buildCreateTable(inScope *scope, c *ast.DDL) (outScope *scope)
326
326
327
327
idxDefs := b .buildIndexDefs (inScope , c .TableSpec )
328
328
329
- schema , collation , tblOpts := b .tableSpecToSchema (inScope , outScope , database , strings .ToLower (c .Table .Name .String ()), c .TableSpec , false )
329
+ schema , collation , tblOpts := b .tableSpecToSchemaWithDefaults (inScope , outScope , database , strings .ToLower (c .Table .Name .String ()), c .TableSpec , false , true )
330
330
fkDefs , chDefs := b .buildConstraintsDefs (outScope , c .Table , c .TableSpec )
331
331
332
332
schema .Schema = assignColumnIndexesInSchema (schema .Schema )
@@ -442,7 +442,7 @@ func (b *Builder) buildCreateTableLike(inScope *scope, ct *ast.DDL) *scope {
442
442
var comment string
443
443
outScope := inScope .push ()
444
444
if ct .TableSpec != nil {
445
- pkSch , coll , _ = b .tableSpecToSchema (inScope , outScope , database , strings .ToLower (ct .Table .Name .String ()), ct .TableSpec , false )
445
+ pkSch , coll , _ = b .tableSpecToSchemaWithDefaults (inScope , outScope , database , strings .ToLower (ct .Table .Name .String ()), ct .TableSpec , false , true )
446
446
}
447
447
448
448
var pkOrdinals []int
@@ -1335,6 +1335,11 @@ func validateOnUpdateExprs(col *sql.Column) error {
1335
1335
1336
1336
// TableSpecToSchema creates a sql.Schema from a parsed TableSpec and returns the parsed primary key schema, collation ID, and table comment.
1337
1337
func (b * Builder ) tableSpecToSchema (inScope , outScope * scope , db sql.Database , tableName string , tableSpec * ast.TableSpec , forceInvalidCollation bool ) (sql.PrimaryKeySchema , sql.CollationID , map [string ]interface {}) {
1338
+ return b .tableSpecToSchemaWithDefaults (inScope , outScope , db , tableName , tableSpec , forceInvalidCollation , false )
1339
+ }
1340
+
1341
+ // tableSpecToSchemaWithDefaults is the internal implementation that allows controlling whether to add implicit DEFAULT NULL
1342
+ func (b * Builder ) tableSpecToSchemaWithDefaults (inScope , outScope * scope , db sql.Database , tableName string , tableSpec * ast.TableSpec , forceInvalidCollation bool , addImplicitDefaults bool ) (sql.PrimaryKeySchema , sql.CollationID , map [string ]interface {}) {
1338
1343
// TODO: helper method?
1339
1344
tblOpts := make (map [string ]interface {})
1340
1345
for _ , tblOpt := range tableSpec .TableOpts {
@@ -1419,7 +1424,7 @@ func (b *Builder) tableSpecToSchema(inScope, outScope *scope, db sql.Database, t
1419
1424
1420
1425
for i , def := range defaults {
1421
1426
schema [i ].Default = b .convertDefaultExpression (outScope , def , schema [i ].Type , schema [i ].Nullable )
1422
- if schema [i ].Default == nil && schema [i ].Nullable {
1427
+ if addImplicitDefaults && schema [i ].Default == nil && schema [i ].Nullable {
1423
1428
schema [i ].Default = & sql.ColumnDefaultValue {
1424
1429
Expr : b .buildScalar (inScope , & ast.NullVal {}),
1425
1430
OutType : types .Null ,
0 commit comments