Overview
Introspection enables:- Greenfield projects - Generate Prisma schema from an existing database
- Schema updates - Detect changes made outside Prisma Migrate
- Database-first workflows - Use native DB tools while keeping Prisma schema in sync
- Migration generation - Understand current state to diff against desired state
Introspection is read-only. It never modifies your database, only reads its structure.
How Introspection Works
The Schema Engine performs introspection through connector-specific implementations:1. System Catalog Queries
Each connector queries database-specific system tables:- PostgreSQL:
information_schema,pg_catalog - MySQL:
information_schema,SHOWcommands - SQL Server:
sys.tables,sys.columns, etc. - SQLite:
sqlite_master,PRAGMAcommands - MongoDB: Collection scanning and sampling
2. Schema Extraction
The connector builds an internalDatabaseSchema representation containing:
- Tables and views
- Columns with types, nullability, defaults
- Primary keys and unique constraints
- Foreign key relationships
- Indexes
- Enums (where supported)
- Sequences and defaults
- Database-specific attributes
3. Datamodel Rendering
Thedatamodel-renderer crate transforms the DatabaseSchema into PSL (Prisma Schema Language):
Introspection Result
TheIntrospectionResult structure returned by connectors contains:
View Definitions
When the views preview feature is enabled:Why are views separate?
Why are views separate?
Views are stored separately because:
- The SQL definition needs to be preserved exactly
- Views might use database-specific syntax
- They’re optional (requires preview feature)
- They need special handling during migration generation
Introspection Features
Multi-Schema Support
With themultiSchema preview feature, introspection can handle databases with multiple schemas:
- Extracts schema/namespace information
- Preserves schema qualifiers in relation definitions
- Respects schema filters
Composite Types
For databases supporting composite types (PostgreSQL, MongoDB):IntrospectionContext:
Relation Inference
The Schema Engine infers relations from:- Foreign key constraints (explicit)
- Naming conventions (implicit)
- Join tables (many-to-many)
Index and Constraint Discovery
Introspection captures:- Primary keys:
@id,@@id - Unique constraints:
@unique,@@unique - Indexes:
@@index - Foreign keys:
@relation - Check constraints: (where supported)
- Default values:
@default()
Introspection Warnings
Introspection may generate warnings for:Unsupported Types
Types that don’t map cleanly to Prisma:Naming Collisions
When database names conflict with Prisma reserved words:Missing Relations
Foreign keys without corresponding tables:Re-introspection Changes
When re-introspecting an existing schema:Introspection Context
TheIntrospectionContext controls behavior:
Re-introspection
Whenprevious_schema is provided:
- Preserves custom model/field names (via
@map,@@map) - Keeps relation names
- Retains comments and documentation
- Warns about unavoidable changes
SQL Introspection
TheintrospectSql method analyzes SQL queries and returns type information:
How SQL introspection works
How SQL introspection works
- Parse the SQL query
- Extract parameter placeholders
- Determine result column types
- Map database types to TypeScript types
- Generate TypeScript definitions
Schema Filters
Introspection respectsSchemaFilter to limit what’s introspected:
- Excluding internal/system tables
- Focusing on specific schemas
- Ignoring legacy tables
- Performance optimization
JSON-RPC Method
Introspection is exposed via theintrospect JSON-RPC method:
force: Re-introspect even if schema existscompositeTypeDepth: How deep to introspect composite types (-1 = infinite)namespaces: List of schemas to introspect
Implementation Details
Connector Trait
All connectors implement:Key Files
- Trait definition:
schema-engine/connectors/schema-connector/src/schema_connector.rs - SQL implementation:
schema-engine/connectors/sql-schema-connector/src/introspection/ - MongoDB implementation:
schema-engine/connectors/mongodb-schema-connector/src/ - Datamodel rendering:
schema-engine/datamodel-renderer/src/
Best Practices
1. Commit Before Introspecting
Always commit your current schema before running introspection:2. Review Generated Schema
Carefully review:- Inferred relations (especially without FK constraints)
- Type mappings
- Naming changes
- Missing or commented-out fields
3. Add Custom Attributes
After introspection, add:@mapand@@mapfor readable names- Relation names
- Comments and documentation
- Validation rules
4. Use Schema Filters
Exclude unnecessary tables:Schema filtering is configured via the JSON-RPC API, not in the schema file itself.
Related Documentation
Schema Diffing
Learn how introspection feeds into migration generation
Migration System
Understand how introspected schemas interact with migrations