Skip to main content
Introspection is the process of analyzing an existing database and generating a Prisma schema that represents its structure. The Schema Engine can introspect tables, columns, indexes, relations, views, and database-specific features.

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, SHOW commands
  • SQL Server: sys.tables, sys.columns, etc.
  • SQLite: sqlite_master, PRAGMA commands
  • MongoDB: Collection scanning and sampling

2. Schema Extraction

The connector builds an internal DatabaseSchema 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

The datamodel-renderer crate transforms the DatabaseSchema into PSL (Prisma Schema Language):

Introspection Result

The IntrospectionResult structure returned by connectors contains:

View Definitions

When the views preview feature is enabled:
Views are stored separately because:
  1. The SQL definition needs to be preserved exactly
  2. Views might use database-specific syntax
  3. They’re optional (requires preview feature)
  4. They need special handling during migration generation

Introspection Features

Multi-Schema Support

With the multiSchema 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):
The introspection depth is configurable via IntrospectionContext:

Relation Inference

The Schema Engine infers relations from:
  • Foreign key constraints (explicit)
  • Naming conventions (implicit)
  • Join tables (many-to-many)
Implicit relations (without foreign keys) are inferred based on naming patterns. Always verify inferred relations match your intent.

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:
These fields are commented out in the schema:

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:
Re-introspection overwrites your Prisma schema. Always commit changes before re-introspecting, or use version control.

Introspection Context

The IntrospectionContext controls behavior:

Re-introspection

When previous_schema is provided:
  • Preserves custom model/field names (via @map, @@map)
  • Keeps relation names
  • Retains comments and documentation
  • Warns about unavoidable changes

SQL Introspection

The introspectSql method analyzes SQL queries and returns type information:
This powers Prisma’s TypedSQL feature, enabling type-safe raw SQL queries.
  1. Parse the SQL query
  2. Extract parameter placeholders
  3. Determine result column types
  4. Map database types to TypeScript types
  5. Generate TypeScript definitions
This is database-specific - each connector implements its own SQL parser/analyzer.

Schema Filters

Introspection respects SchemaFilter to limit what’s introspected:
Useful for:
  • Excluding internal/system tables
  • Focusing on specific schemas
  • Ignoring legacy tables
  • Performance optimization

JSON-RPC Method

Introspection is exposed via the introspect JSON-RPC method:
Parameters:
  • force: Re-introspect even if schema exists
  • compositeTypeDepth: 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:
  • @map and @@map for 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.

Schema Diffing

Learn how introspection feeds into migration generation

Migration System

Understand how introspected schemas interact with migrations