Codegen Troubleshooting
Purpose: Common issues and solutions
Last Updated: 2024-11-28
Quick Fixes
| Problem | Solution | Command |
|---|---|---|
| Type errors | Regenerate code | pnpm db:codegen |
| Missing function | Check schema included | pnpm db:codegen --verbose |
| Validation error | Check parameter types | See error message |
| Stale types | Force regeneration | pnpm db:codegen --force |
| IDE not updating | Restart TS server | Cmd/Ctrl + Shift + P → "Restart TS Server" |
Connection Issues
Cannot Connect to Database
Symptoms:
Error: Connection refused
ECONNREFUSED 127.0.0.1:54320
Solutions:
- Check database is running:
docker ps | grep postgres
- Start database if not running:
docker compose up -d postgres
- Verify connection string:
echo $DATABASE_URL
- Test connection directly:
psql -h localhost -p 54320 -U savvi -d savvi_studio -c "SELECT 1"
Wrong Database
Symptoms:
Error: relation "my_schema.my_function" does not exist
Solutions:
- Check you're connected to correct database
- Verify schema loaded
- Reset database if needed:
pnpm db:reset
Generation Issues
Function Not Generated
Symptoms: Function exists in database but not in generated code
Solutions:
- Verify function exists in database
- Check schema filter
- Force regeneration:
rm -rf src/__generated__/my_schema
pnpm db:codegen
Type Not Generated
Symptoms: Custom type exists but schema not generated
Solutions:
- Verify type exists in database
- Check type category (must be enum, composite, or domain)
- Verify schema loaded
Circular Dependency Error
Symptoms:
Error: Circular dependency detected: TypeA -> TypeB -> TypeA
Solution: Generated code handles circular refs automatically using z.lazy(). If you see this error, it's likely a bug in the codegen system.
Import Issues
Module Not Found
Symptoms:
Error: Cannot find module '@db/my_schema'
Solutions:
- Restart TypeScript server (VS Code: Cmd/Ctrl + Shift + P → "Restart TS Server")
- Verify path alias in tsconfig.json
- Check generated files exist
- Regenerate if missing:
pnpm db:codegen
Wrong Import Path
Symptoms:
Error: Module '"@db/graph"' has no exported member 'myFunction'
Solutions:
- Check function name (camelCase in TypeScript, e.g.,
create_resource→createResource) - Import from correct schema
- Check exports in index file
Type Errors
Type Mismatch After Update
Symptoms:
Error: Type 'string' is not assignable to type 'bigint'
Solutions:
- Regenerate types:
pnpm db:codegen
- Find all call sites:
grep -r "myFunction" src/
- Update function calls to match new signature
Missing Required Parameter
Symptoms:
Error: Property 'p_email' is missing in type
Solutions:
- Add required parameter to function call
- Check if parameter should be optional in SQL (add DEFAULT)
Validation Errors
ZodError at Runtime
Symptoms:
ZodError: Invalid input
[{ code: 'invalid_type', expected: 'string', received: 'number', path: ['p_email'] }]
Solutions:
- Check error details to see which field failed
- Verify param types match expected types
- Check for null/undefined vs nullable fields
Unexpected Database Output
Symptoms:
ZodError: Database returned unexpected data
Solutions:
- Regenerate to sync with current schema:
pnpm db:codegen
- Verify function return type matches actual output
- Test function directly in psql
Performance Issues
Slow Codegen
Symptoms: Codegen takes > 5 seconds
Solutions:
- Generate specific schemas only:
pnpm db:codegen --schema graph
- Use caching:
pnpm db:codegen --cache
- Check database performance
Large Generated Files
Symptoms: Generated files > 1MB
Solutions:
- Split large schemas into multiple smaller schemas
- Review function count per schema
Build Issues
Generated Code Doesn't Compile
Symptoms:
Error: TS2322: Type 'unknown' is not assignable to type 'string'
Solutions:
- Check TypeScript version (should be >= 4.7)
- Verify Zod version (should be >= 3.0)
- Clean and rebuild:
rm -rf src/__generated__
pnpm db:codegen
pnpm tsc --noEmit
Module Resolution Errors
Symptoms:
Error: Cannot resolve module specifier
Solutions:
- Check tsconfig.json module resolution settings
- Restart development server
- Verify path aliases configured correctly
Testing Issues
Mock Client Not Working
Symptoms: Tests fail with "query is not a function"
Solution: Ensure mock client has proper structure with query method.
Integration Tests Failing
Symptoms: Tests work locally but fail in CI
Solutions:
- Check test database setup in CI config
- Ensure database is ready before running tests
- Run migrations before tests
- Generate code in CI pipeline
Getting More Help
Enable Verbose Logging
pnpm db:codegen --verbose
Check Codegen Logs
# Review introspection queries
pnpm db:codegen --verbose 2>&1 | grep "SELECT"
# Review generated files
pnpm db:codegen --verbose 2>&1 | grep "Writing"
Report Issues
Include in bug reports:
- PostgreSQL version
- Node.js version
- Codegen command used
- Error messages (full stack trace)
- Relevant SQL definitions
- Example of generated code (if applicable)
Detailed Examples
For detailed troubleshooting examples with code, see:
- Troubleshooting Examples - Code examples for common issues
- CLI Examples - Debugging commands and workflows
Related Documentation
- Usage - Running codegen
- Overview - System architecture
- Type System - Type mappings
- Function Wrappers - Using generated code
Pro Tip: Most issues are solved by regenerating code with pnpm db:codegen and restarting the TypeScript server.