Overview
Prisma Schema Language (PSL) is the domain-specific language used to define your data model, database connection, and generator configuration in Prisma. This component is responsible for parsing, validating, and analyzing Prisma schema files.PSL is the foundation of the entire Prisma stack. Every component—from the Schema Engine to the Query Compiler to Prisma Format—relies on PSL to understand your data model.
What is PSL?
PSL is a declarative language that lets you define:- Data models: Tables, collections, and their relationships
- Fields: Columns with types, attributes, and constraints
- Datasources: Database connection configuration
- Generators: Client and other code generation settings
- Enums: Enumerated types
- Composite types: Embedded document types (MongoDB)
Example Schema
Architecture
The PSL implementation follows a layered architecture with clear separation of concerns:Diagnostics
Error and warning collection, pretty printing, span tracking
Schema AST
Abstract syntax tree representation of the parsed schema
Parser Database
Semantic analysis and validation of the schema
PSL Core
Public API, configuration, and high-level operations
Dependency Graph
Crate Organization
The PSL codebase is organized into focused crates:diagnostics
Handles errors, warnings, and diagnostic messages:
- Error collection during parsing
- Pretty-printed error messages with source context
- Span tracking for precise error locations
- Warning accumulation
schema-ast
Contains the Abstract Syntax Tree definitions:
- Raw schema parsing
- AST node definitions
- Syntax validation
- Comment preservation
parser-database
Provides semantic analysis and validation:
- Attribute validation
- Relation inference
- Type checking
- Constraint validation
- Index and unique constraint handling
psl-core
High-level API and configuration:
- Schema validation entry point
- Configuration parsing (datasources, generators)
- Connector selection
- Preview feature management
builtin-connectors
Database-specific validation and type systems:
- PostgreSQL connector
- MySQL connector
- SQLite connector
- SQL Server connector
- MongoDB connector
- CockroachDB connector
- Native type mappings
- Capability definitions
- Database-specific validation rules
- Default values and constraints
psl (Public API)
The main entry point used by other Prisma components:
Core Functionality
Schema Validation
The main validation flow:Multi-File Schemas
Prisma supports splitting schemas across multiple files:Configuration Parsing
Extract datasources and generators without full validation:Schema Reformatting
Format Prisma schemas consistently:Validation Features
Type Checking
Validates field types, ensures type compatibility, checks native type mappings
Relation Validation
Infers implicit relations, validates explicit relations, checks referential actions
Attribute Validation
Validates
@id, @unique, @default, @relation, and all other attributesConstraint Checking
Ensures unique constraints, validates indexes, checks composite keys
Attribute System
PSL supports field-level and block-level attributes: Field attributes (single@):
@@):
Relation Inference
PSL can automatically infer simple relations:Diagnostics and Error Reporting
PSL provides excellent error messages with source context:Pretty Printing
The diagnostics crate provides formatted output:Connector System
Connectors provide database-specific behavior:Capabilities
Each connector declares its capabilities:AutoIncrementEnumsJsonFullTextSearchMultiSchemaNamedForeignKeysScalarLists- And many more…
Preview Features
PSL supports preview features for experimental functionality:Testing
PSL has three main test entry points:1. Validation Tests
Declarative tests inpsl/tests/validation/:
2. Reformat Tests
Tests inpsl/tests/reformat/:
3. Unit Tests
Regular Rust tests inpsl/tests/datamodel_tests.rs:
Configuration
Datasource Configuration
Generator Configuration
Native Type Mappings
Each connector maps Prisma types to database-native types:Common Use Cases
Schema Validation in CI
Programmatic Schema Generation
Extracting Model Information
Prisma 7 Changes
PSL now rejects these properties with helpful error messages:Related Components
- Query Compiler - Uses PSL to understand the data model
- Schema Engine - Uses PSL for migration generation
- Prisma Format - Uses PSL for formatting and LSP features
Source Code
Explore the PSL source code:- Main crate:
psl/psl/ - Core logic:
psl/psl-core/src/ - Parser database:
psl/parser-database/ - Diagnostics:
psl/diagnostics/ - Contributing guide:
psl/CONTRIBUTING.md - Repository: prisma/prisma-engines