Skip to content

Commit fcdd42a

Browse files
elianddbclaude
andcommitted
Fix enum error handling for strict mode vs regular operations
- Fixed enum Convert method to return ErrDataTruncatedForColumn for strict mode 0 values - Removed inappropriate ErrConvertingToEnum -> ErrDataTruncatedForColumnAtRow transformation - Restored proper error messages for regular enum validation failures - Fixed INSERT IGNORE enum conversion to use proper empty string handling 🎯 Strict mode enum validation now works correctly for INSERT operations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 2c8734e commit fcdd42a

File tree

2 files changed

+2
-29
lines changed

2 files changed

+2
-29
lines changed

sql/rowexec/insert.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,8 @@ func (i *insertIter) Next(ctx *sql.Context) (returnRow sql.Row, returnErr error)
144144
cErr = types.ErrLengthBeyondLimit.New(row[idx], col.Name)
145145
} else if sql.ErrNotMatchingSRID.Is(cErr) {
146146
cErr = sql.ErrNotMatchingSRIDWithColName.New(col.Name, cErr)
147-
} else if types.ErrConvertingToEnum.Is(cErr) {
148-
cErr = types.ErrDataTruncatedForColumnAtRow.New(col.Name, i.rowCount)
149147
} else if types.ErrDataTruncatedForColumn.Is(cErr) {
150-
cErr = types.ErrDataTruncatedForColumn.New(col.Name, i.rowCount)
148+
cErr = types.ErrDataTruncatedForColumnAtRow.New(col.Name, i.rowCount)
151149
}
152150
return nil, sql.NewWrappedInsertError(origRow, cErr)
153151
}

sql/types/enum.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,6 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
107107
}, nil
108108
}
109109

110-
// isInsertContext determines if we're in an insert operation context where column name and row information
111-
// are available for error reporting, versus other contexts like CREATE TABLE default validation.
112-
func isInsertContext(ctx *sql.Context) bool {
113-
// Check if we're in an insert operation by looking for insert-specific context information
114-
// During insert operations, the context typically has access to row iteration state
115-
if ctx == nil {
116-
return false
117-
}
118-
119-
// Check the query string for INSERT keywords
120-
query := strings.ToUpper(strings.TrimSpace(ctx.Query()))
121-
return strings.HasPrefix(query, "INSERT") || strings.HasPrefix(query, "REPLACE") || strings.Contains(query, "ON DUPLICATE KEY UPDATE")
122-
}
123-
124110
// MustCreateEnumType is the same as CreateEnumType except it panics on errors.
125111
func MustCreateEnumType(values []string, collation sql.CollationID) sql.EnumType {
126112
et, err := CreateEnumType(values, collation)
@@ -186,14 +172,7 @@ func (t EnumType) Convert(ctx context.Context, v interface{}) (interface{}, sql.
186172
// In strict mode, 0 values are not allowed for enums unless empty string is explicitly defined
187173
// Check if empty string is an actual enum value (not just the default index 0)
188174
if t.IndexOf("") == -1 {
189-
// For insert operations, return the specific error that will be filled in with column/row info
190-
// For other operations (e.g., CREATE TABLE default validation), return the generic error
191-
// Check if this is an insert context by looking for a wrapped insert error or row count info
192-
if isInsertContext(sqlCtx) {
193-
return nil, sql.InRange, ErrDataTruncatedForColumn.New("", 0)
194-
}
195-
// For default value validation and other contexts, return the generic error
196-
return nil, sql.InRange, ErrConvertingToEnum.New(v)
175+
return nil, sql.InRange, ErrDataTruncatedForColumn.New("")
197176
}
198177
// Empty string is a defined enum value, allow it
199178
return uint16(0), sql.InRange, nil
@@ -344,10 +323,6 @@ func (t EnumType) Collation() sql.CollationID {
344323

345324
// IndexOf implements EnumType interface.
346325
func (t EnumType) IndexOf(v string) int {
347-
// Empty string always maps to index 0 for INSERT IGNORE compatibility
348-
if v == "" {
349-
return 0
350-
}
351326
if idx, ok := t.valToIdx[v]; ok {
352327
return idx
353328
}

0 commit comments

Comments
 (0)