Data Layer constraints on Project relationships
Copy page
How the Inkeep Agent Framework ensures data integrity with project constraints
Project Constraints and Validation
The Inkeep Agent Framework implements multiple layers of validation to ensure that all agents and resources are associated with valid projects.
Database-Level Constraints
Foreign Key Constraints
All tables that reference projects include foreign key constraints to ensure referential integrity:
This ensures that:
- No data can be inserted for non-existent projects
- Deleting a project cascades to remove all associated resources
- Data integrity is maintained at the database level
Tables with Project Constraints
The following tables have foreign key constraints to the projects table:
agent- Agent definitions (top-level Agents)sub_agents- Sub Agent configurationssub_agent_relations- Relationships between Sub Agentstools- Tool configurationscontext_configs- Context configurationsexternal_agents- External agent references- And more...
Runtime Validation
CLI Validation
The Inkeep CLI validates project existence before pushing agents:
Data Access Layer Validation
The core package provides validation utilities for runtime checks:
Wrapped Operations
Data access functions can be wrapped with automatic validation:
Type-Safe Scope Helpers
The data access layer uses type-safe scope helpers to enforce consistent project scoping across all queries. This prevents accidental data leakage between tenants and projects.
ScopedTable Pattern
All DAL queries use scope helper functions instead of manual eq() WHERE clauses:
Available Scope Helpers
| Helper | Scope Level | Required Fields |
|---|---|---|
tenantScopedWhere | Tenant | tenantId |
projectScopedWhere | Project | tenantId, projectId |
agentScopedWhere | Agent | tenantId, projectId, agentId |
subAgentScopedWhere | Sub Agent | tenantId, projectId, agentId, subAgentId |
toolScopedWhere | Tool | tenantId, projectId, toolId |
Security Benefits
- Compile-time safety: TypeScript enforces that all required scope fields are provided
- Consistent scoping: All queries automatically include the correct tenant/project filters
- Reduced errors: Prevents accidentally omitting scope conditions
Implementation Details
Schema Definition
Enabling Foreign Keys in SQLite
SQLite requires explicit enabling of foreign key constraints:
This should be set when creating the database connection:
Error Handling
When a constraint violation occurs, appropriate error messages guide users:
CLI Error
Database Error
Best Practices
- Always validate at multiple levels: Database constraints, runtime validation, and UI validation
- Provide clear error messages: Help users understand what went wrong and how to fix it
- Offer solutions: When a project doesn't exist, offer to create it
- Use transactions: Ensure atomicity when creating projects and related resources
- Test constraints: Verify that constraints work as expected in tests
- Use scope helpers: Always use
projectScopedWhereand related helpers instead of manualeq()clauses for consistent data isolation
Migration Guide
For existing databases without constraints:
- Backup your database before applying migrations
- Check for orphaned data: Find resources referencing non-existent projects
- Clean up orphaned data or create missing projects
- Apply foreign key constraints using migrations
- Enable foreign key enforcement in your database connection
Benefits
- Data Integrity: Prevents orphaned data and maintains consistency
- Clear User Experience: Users are guided to create projects when needed
- Easier Debugging: Constraint violations are caught early
- Simplified Cleanup: Cascading deletes remove all related data
- Better Documentation: Constraints document relationships in the schema