Skip to content

Commit 7d1c956

Browse files
elianddbclaude
andcommitted
Restrict DEFAULT NULL logic to CREATE TABLE operations only
Modified tableSpecToSchema to only add implicit DEFAULT NULL when called for CREATE TABLE operations, not for ALTER TABLE or other schema processing operations. This prevents unintended side effects on existing table modifications. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8cab33a commit 7d1c956

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

sql/planbuilder/ddl.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (b *Builder) buildCreateTable(inScope *scope, c *ast.DDL) (outScope *scope)
326326

327327
idxDefs := b.buildIndexDefs(inScope, c.TableSpec)
328328

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)
330330
fkDefs, chDefs := b.buildConstraintsDefs(outScope, c.Table, c.TableSpec)
331331

332332
schema.Schema = assignColumnIndexesInSchema(schema.Schema)
@@ -442,7 +442,7 @@ func (b *Builder) buildCreateTableLike(inScope *scope, ct *ast.DDL) *scope {
442442
var comment string
443443
outScope := inScope.push()
444444
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)
446446
}
447447

448448
var pkOrdinals []int
@@ -1335,6 +1335,11 @@ func validateOnUpdateExprs(col *sql.Column) error {
13351335

13361336
// TableSpecToSchema creates a sql.Schema from a parsed TableSpec and returns the parsed primary key schema, collation ID, and table comment.
13371337
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{}) {
13381343
// TODO: helper method?
13391344
tblOpts := make(map[string]interface{})
13401345
for _, tblOpt := range tableSpec.TableOpts {
@@ -1419,7 +1424,7 @@ func (b *Builder) tableSpecToSchema(inScope, outScope *scope, db sql.Database, t
14191424

14201425
for i, def := range defaults {
14211426
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 {
14231428
schema[i].Default = &sql.ColumnDefaultValue{
14241429
Expr: b.buildScalar(inScope, &ast.NullVal{}),
14251430
OutType: types.Null,

0 commit comments

Comments
 (0)