Troubleshooting

Quick solutions for common issues when working with CQL.

Setup & Installation Issues

Shard Installation Fails

Error: Failed to resolve dependencies

Solution:

  1. Check Crystal version compatibility: crystal --version

  2. Update shard.yml with compatible version:

    dependencies:
      cql:
        github: azutoolkit/cql
        version: "~> 1.0"
  3. Install with: shards install

Database Driver Not Found

Error: can't load file 'pg'

Solution: Add the database driver to shard.yml:

dependencies:
  # PostgreSQL
  pg:
    github: will/crystal-pg
  # MySQL
  mysql:
    github: crystal-lang/crystal-mysql
  # SQLite
  sqlite3:
    github: crystal-lang/crystal-sqlite3

Database Connection Problems

Connection Refused

Error: Connection refused (Errno)

Solutions:

  1. Verify database is running:

  2. Check connection string format:

  3. Test connection manually:

Authentication Failed

Error: password authentication failed

Solutions:

  1. Test credentials manually:

  2. Use environment variables:

  3. Create database user if needed:

Schema & Migration Issues

Column Not Found Error

Error: undefined method 'name' for #<NamedTuple(id: Int64)>

Solution: Ensure schema matches database:

Verify with: crystal run db/migrate.cr

Migration Fails - Column Exists

Error: column "email" already exists

Solution: Check before adding columns:

Query & Model Issues

Type Mismatch Errors

Error: no overload matches 'User#new'

Solution: Use proper type casting:

Records Not Found

Error: DB::NoResultsError

  1. Check if migration was run:

  2. Verify table structure:

❌ Issue: Migration Fails with Column Already Exists

Solutions:

  1. Check existing columns before adding:

  2. Use conditional migrations:

  3. Reset and rebuild database (development only):


🔍 Query & Model Issues

❌ Issue: Type Mismatch Errors

Solution:

  1. Ensure proper type casting:

  2. Use safe casting with validation:

❌ Issue: Records Not Found

Debugging Steps:

  1. Use safe find methods:

  2. Check your query conditions:

  3. Verify data exists:

❌ Issue: Association Loading Problems

Solution:

  1. Verify association is defined:

  2. Check foreign key setup:


⚠️ Runtime Errors

❌ Issue: Validation Errors Not Displayed

Solution:

  1. Always check validation errors:

  2. Use save! for development debugging:

❌ Issue: Transaction Rollback Problems

Solution:

  1. Ensure exceptions are raised:

  2. Handle rollback explicitly:


⚡ Performance Issues

❌ Issue: Slow Queries

Debugging Steps:

  1. Enable query logging:

  2. Analyze query performance:

  3. Check for N+1 queries:

❌ Issue: High Memory Usage

Solutions:

  1. Use pagination for large datasets:

  2. Use select to limit columns:


🛠️ Development Tips

🔍 Debugging Techniques

  1. Enable SQL logging:

  2. Inspect model state:

  3. Check database state:

⚡ Quick Fixes

  1. Reset database in development:

  2. Clear and rebuild schema cache:

  3. Check for pending migrations:


🆘 Getting More Help

📚 Additional Resources

🐛 Reporting Issues

When reporting issues, please include:

  1. Crystal version: crystal --version

  2. CQL version: Check your shard.yml

  3. Database type and version

  4. Minimal reproduction code

  5. Full error message and stack trace

  6. Expected vs actual behavior

💡 Best Practices

  • Use transactions for multi-step operations

  • Validate input before database operations

  • Handle exceptions gracefully in production

  • Use environment variables for sensitive configuration

  • Test database operations with proper fixtures

  • Monitor query performance in production

  • Keep migrations reversible when possible


💡 Pro Tip: Most CQL issues are related to schema mismatches or missing validations. Always verify your schema definitions match your database structure and use proper error handling patterns.

Still stuck? Check the FAQ section or reach out to the community for help! 🤝

Last updated

Was this helpful?