Managing Schema Introspection
1. Pulling Existing Schema
npx prisma db pull
| Aspect | Detail |
|---|---|
| Generates | Models from current DB tables |
| Preserves | Custom @map, @@map annotations on rerun |
2. Mapping Table Names
| Original | Result |
|---|---|
| snake_case | PascalCase model + @@map |
| Plural | Singularized model name by convention |
3. Mapping Column Names
| Original | Result |
|---|---|
| snake_case | camelCase + @map |
| Reserved word | Renamed with @map |
4. Handling Unsupported Types
| Outcome | Detail |
|---|---|
| Marked Unsupported | field Unsupported("polygon") |
| Hidden from client | Field added as @ignore |
| Workaround | Use raw SQL to access |
5. Using Custom Type Mappings
| Case | Approach |
|---|---|
| Citext → String | Combine String @db.Citext |
| Domain types | Map manually to scalar |
6. Preserving Manual Schema Changes
| Preserved | Detail |
|---|---|
| @@map / @map | Kept on re-pull |
| Custom relation names | Kept |
| Comments | /// comments preserved |
7. Updating Schema from Database
| Workflow | Detail |
|---|---|
| DB-first | db pull + prisma generate |
| Hybrid | Periodically pull, then maintain schema |
8. Handling Database Views
| Aspect | Detail |
|---|---|
| Preview | previewFeatures = ["views"] |
| Syntax | view Name { ... } |
| Migrations | View SQL stored under prisma/views/ |
9. Managing Legacy Database Schemas
| Issue | Mitigation |
|---|---|
| Missing PK | Add via @@ignore or composite PK |
| Cyclic FKs | Mark onDelete: NoAction |
| Mixed naming | Use @map/@@map |
10. Syncing Schema Changes
| Command | Use |
|---|---|
db push | Push schema without migration history |
migrate dev | Versioned sync |
db pull | Pull DB → schema |