Skip to main content

Overview

Update operations in ChameleonDB modify existing records with built-in safety guards. All updates require a WHERE clause to prevent accidental full-table modifications.
Safety First: ChameleonDB requires a Filter() clause for all updates. Attempting to update without a filter will result in a SafetyError.This prevents catastrophic mistakes like:

Basic Update

Update a single record by ID:

Update Multiple Fields

Chain multiple .Set() calls to update several fields:

Update with Complex Filters

Use multiple filters to target specific records:

Conditional Updates

Update records based on date ranges or other conditions:

Full-Table Updates (Use with Caution)

If you genuinely need to update all records in a table, use ForceUpdateAll():
Dangerous Operation: ForceUpdateAll() bypasses the safety guard and updates every record in the table. Use this only when absolutely necessary and ensure you:
  • Understand the impact
  • Have backups in place
  • Are in the correct environment (not production)
  • Have tested the operation first

Update Result

The UpdateResult type provides information about the operation:

Error Handling

Handle common update errors:

Debug Mode

Inspect generated SQL with .Debug():

Repository Pattern

Recommended pattern for encapsulating update logic:

Best Practices

Always Use Specific Filters

Check Affected Rows

Validate Before Update

Use Transactions for Multiple Updates

Common Patterns

Toggle Boolean Fields

Bulk Status Updates

Partial Updates

HTTP Handler Example

Next Steps

Delete Operations

Remove records safely with WHERE clause requirements

Safety Guards

Learn about ChameleonDB’s mutation safety features

Insert Operations

Create new records with validation

Error Handling

Complete error handling reference