Common CQL errors and their meanings.
Raised when model validations fail.
begin
user.validate!
rescue CQL::ActiveRecord::Validations::ValidationError => ex
puts ex.message # Contains validation error messages
endCommon causes:
- Required field is empty
- Value doesn't match format
- Value outside allowed range
Solution: Check model.errors for specific field errors.
Raised when a concurrent update is detected.
begin
user.update!
rescue CQL::OptimisticLockError
user.reload! # Get latest version
# Re-apply changes and retry
endCommon causes:
- Another process updated the record
- Version column mismatch
Solution: Reload the record and retry the operation.
Raised when find! cannot locate a record.
begin
user = User.find!(999)
rescue CQL::RecordNotFound
puts "User not found"
endSolution: Use find (returns nil) instead, or handle the exception.
Raised when save! or create! fails validation.
begin
User.create!(name: "", email: "")
rescue CQL::RecordInvalid => ex
puts "Failed: #{ex.message}"
endSolution: Check validations before saving, or use non-bang methods.
Raised when database connection fails.
Common causes:
- Database server not running
- Wrong connection string
- Network issues
- Authentication failure
Solution: Verify connection settings and database availability.
Raised when a SQL query fails.
Common causes:
- Syntax error in raw SQL
- Reference to non-existent column
- Type mismatch
Solution: Check the SQL being generated and table structure.
Raised when a migration fails.
Common causes:
- Table already exists
- Column already exists
- Foreign key constraint violation
- Invalid SQL syntax
Solution: Check migration status and database state.
Raised when attempting to rollback an irreversible migration.
def down
raise "Cannot rollback: data would be lost"
endSolution: Manually handle the rollback or skip it.
Raised when referencing a table that doesn't exist.
Solution: Ensure migrations have run and table exists.
Raised when referencing a column that doesn't exist.
Solution: Check column name spelling and run migrations.
Raised for invalid configuration settings.
Common causes:
- Missing required configuration
- Invalid adapter name
- Malformed connection string
Solution: Review configuration settings.
Log.setup do |c|
c.bind("cql.*", :debug, Log::IOBackend.new)
endbegin
# operation
rescue ex
puts "Error type: #{ex.class}"
puts "Message: #{ex.message}"
puts "Backtrace: #{ex.backtrace.first(5).join("\n")}"
endunless model.valid?
model.errors.each do |error|
puts "#{error.field}: #{error.message}"
end
end