This error occurs during Prisma introspection when your database has cross-schema foreign key references or multiple schemas that aren't properly configured in your Prisma schema file.
The P4002 error is thrown by Prisma's introspection engine when it detects inconsistencies in your database schema that it cannot automatically resolve. This most commonly happens when you have foreign key relationships that span across different database schemas (like PostgreSQL's `public` and `auth` schemas), or when tables reference schemas that aren't declared in your Prisma configuration. Prisma introspection scans your database to generate a schema file, but it requires explicit configuration when dealing with multi-schema databases. Without proper setup, it cannot determine how to handle cross-schema relationships and will fail with this error. This error is particularly common when using managed database services like Supabase, which creates multiple schemas by default (such as `auth` for authentication tables and `public` for user tables), and when these schemas have foreign key relationships between them.
If you're working with multiple database schemas, enable Prisma's multiSchema preview feature in your schema.prisma file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema"]
}This tells Prisma that your database uses multiple schemas and enables support for cross-schema relationships.
Declare all schemas that contain tables or have foreign key relationships in your datasource block:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["public", "auth"]
}Add every schema that's referenced by foreign keys. For Supabase databases, you typically need both public and auth. For custom setups, include all relevant schema names (e.g., ["public", "auth", "analytics"]).
If the error mentions specific tables not being found, temporarily remove those model definitions from your schema.prisma file:
// Remove or comment out conflicting models
// model User {
// id String @id
// email String
// }This allows Prisma to regenerate them correctly during introspection without conflicts.
Execute the introspection command to regenerate your schema:
npx prisma db pullPrisma will now properly handle cross-schema relationships and generate models with correct @@schema() attributes:
model User {
id String @id
authUser AuthUser @relation(...)
@@schema("public")
}
model AuthUser {
id String @id
users User[]
@@schema("auth")
}Check that your schema.prisma now includes:
1. @@schema() attributes on models indicating their schema
2. Proper relations between cross-schema models
3. All tables from referenced schemas
If issues persist, verify database permissions:
-- Check which schemas your database user can access
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema');Supabase-specific considerations: Supabase creates several internal schemas (auth, storage, realtime, etc.). You only need to include schemas in your Prisma configuration that you're actually referencing with foreign keys. For most applications, ["public", "auth"] is sufficient.
Handling ignored schemas: If you want Prisma to ignore certain schemas entirely, you can use the --force flag, but this may break existing foreign key relationships. Instead, prefer explicitly listing only the schemas you need.
SQL Server considerations: This error can also occur with SQL Server when using different database schemas. The solution is the same: enable multiSchema and list all schemas in the datasource configuration.
TimescaleDB compatibility: TimescaleDB creates hypertables in the _timescaledb_internal schema with foreign keys to your main schema. If using TimescaleDB, add this schema to your configuration or consider restructuring your foreign key relationships.
Migration workflow: When working with multi-schema databases, always use prisma db pull first to sync your schema, then create migrations with prisma migrate dev. Attempting to create migrations before properly configuring schemas will fail.
P6005: Invalid parameters (Pulse)
How to fix "P6005: Invalid parameters (Pulse)" in Prisma
P2011: Null constraint violation on the field
How to fix "P2011: Null constraint violation" in Prisma
P2009: Failed to validate the query: {validation_error}
How to fix "P2009: Failed to validate the query" in Prisma
P2007: Data validation error
How to fix "P2007: Data validation error" in Prisma
P1013: The provided database string is invalid
The provided database string is invalid