Skip to content

Commit 3cb9fee

Browse files
elianddbclaude
andcommitted
Fix CREATE TABLE enum default validation to show proper column names
- Add early validation in tableSpecToSchema to catch enum default 0 before conversion - Prevents columndefault.go from returning "(unknown)" in error messages - Enhanced validateDefaultExprs to handle enum defaults with proper column context - Now matches MySQL behavior exactly: "Invalid default value for 'column_name'" Resolves the remaining column name issue for CREATE TABLE statements while maintaining all existing INSERT validation functionality. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1f84fda commit 3cb9fee

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

sql/planbuilder/ddl.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,18 +1298,36 @@ func validateDefaultExprs(col *sql.Column) error {
12981298
if col.Default == nil {
12991299
return nil
13001300
}
1301-
if !(types.IsDatetimeType(col.Type) || types.IsTimestampType(col.Type)) {
1302-
return nil
1303-
}
1304-
var colPrec int
1305-
if dt, ok := col.Type.(sql.DatetimeType); ok {
1306-
colPrec = dt.Precision()
1307-
}
1308-
if isValid, err := validatePrec(col.Default.Expr, colPrec); err != nil {
1309-
return err
1310-
} else if !isValid {
1311-
return sql.ErrInvalidColumnDefaultValue.New(col.Name)
1301+
1302+
// Validate datetime/timestamp precision
1303+
if types.IsDatetimeType(col.Type) || types.IsTimestampType(col.Type) {
1304+
var colPrec int
1305+
if dt, ok := col.Type.(sql.DatetimeType); ok {
1306+
colPrec = dt.Precision()
1307+
}
1308+
if isValid, err := validatePrec(col.Default.Expr, colPrec); err != nil {
1309+
return err
1310+
} else if !isValid {
1311+
return sql.ErrInvalidColumnDefaultValue.New(col.Name)
1312+
}
1313+
}
1314+
1315+
// Validate enum defaults in strict mode
1316+
if types.IsEnum(col.Type) {
1317+
// Try to evaluate the default value and convert it
1318+
if col.Default.Expr != nil {
1319+
// Check if it's a literal 0 which should fail in strict mode
1320+
if lit, ok := col.Default.Expr.(*expression.Literal); ok {
1321+
if val, err := lit.Eval(sql.NewEmptyContext(), nil); err == nil {
1322+
if intVal, ok := val.(int64); ok && intVal == 0 {
1323+
// This is a literal 0 default for enum, which MySQL rejects
1324+
return sql.ErrInvalidColumnDefaultValue.New(col.Name)
1325+
}
1326+
}
1327+
}
1328+
}
13121329
}
1330+
13131331
return nil
13141332
}
13151333

@@ -1418,6 +1436,15 @@ func (b *Builder) tableSpecToSchema(inScope, outScope *scope, db sql.Database, t
14181436
}
14191437

14201438
for i, def := range defaults {
1439+
// Early validation for enum default 0 to catch it before conversion
1440+
if def != nil && types.IsEnum(schema[i].Type) {
1441+
if lit, ok := def.(*ast.SQLVal); ok {
1442+
if lit.Type == ast.IntVal && string(lit.Val) == "0" {
1443+
b.handleErr(sql.ErrInvalidColumnDefaultValue.New(schema[i].Name))
1444+
}
1445+
}
1446+
}
1447+
14211448
schema[i].Default = b.convertDefaultExpression(outScope, def, schema[i].Type, schema[i].Nullable)
14221449
err := validateDefaultExprs(schema[i])
14231450
if err != nil {

0 commit comments

Comments
 (0)