Skip to main content

Overview

Insert operations in ChameleonDB provide a safe, validated way to create new records in your database. All inserts go through a three-stage validation pipeline to ensure data integrity.

Basic Insert

The simplest insert requires setting values for all required fields:

Insert with Default Values

Fields with defaults in your schema don’t need to be set:

Validation Pipeline

Every insert goes through three validation stages:

1. Schema Validation

Verifies that:
  • Entity exists in schema
  • All fields are defined
  • Types match schema definitions

2. Constraint Validation

Enforces:
  • NOT NULL constraints
  • UNIQUE constraints
  • Field presence requirements

3. Database Validation

Final checks at database level:
  • Foreign key constraints
  • Unique constraint violations
  • Database-specific rules

Error Handling

ChameleonDB provides typed errors for precise error handling:

Insert with Relations

When inserting records with foreign keys, ensure parent records exist:
If you try to insert a record with an invalid foreign key reference, you’ll get a ForeignKeyError:

Debug Mode

Use .Debug() to see the generated SQL:

Repository Pattern

Recommended pattern for production applications:

Best Practices

Always Generate UUIDs

Validate Before Insert

Handle Errors Gracefully

Next Steps

Update Operations

Learn how to safely update existing records

Delete Operations

Remove records with safety guards

Safety Guards

Understand ChameleonDB’s mutation safety features

Error Handling

Complete error handling reference