Working with Model Attributes
1. Using @@id Attribute
| Aspect | Notes |
|---|---|
| Purpose | Composite primary key |
| Syntax | @@id([userId, roleId]) |
| Options | name, map |
| Lookup | findUnique({ where: { userId_roleId: {...} } }) |
2. Using @@unique Attribute
| Option | Use |
|---|---|
fields | Field array for composite uniqueness |
name | Client-side compound key alias |
map | DB constraint name |
clustered | SQL Server clustered index flag |
3. Using @@index Attribute
| Option | Description |
|---|---|
fields | Indexed columns (ordered) |
name | Client key (rare) |
map | DB index name |
type | Hash | Gin | Gist | SpGist | Brin (PG) |
ops | Operator class per field |
@@index([createdAt(sort: Desc)])
@@index([tags], type: Gin)
4. Using @@map Attribute
| Pattern | Example |
|---|---|
| Snake_case | @@map("user_profiles") |
| Schema-qualified | Use @@schema("auth") + @@map |
5. Using @@ignore Attribute
| When | Effect |
|---|---|
| Unsupported model | Keeps DDL but hides from client |
| After introspection | Auto-added when PK missing |
6. Using @@schema Attribute
| Requirement | Detail |
|---|---|
| Preview | previewFeatures = ["multiSchema"] |
| Datasource | List schemas = ["public","auth"] |
| Usage | @@schema("auth") inside model/enum |
7. Using @@fulltext Attribute
| DB | Notes |
|---|---|
| MySQL | Stable; native FULLTEXT index |
| PostgreSQL | Preview fullTextSearchPostgres |
| Syntax | @@fulltext([title, content]) |
8. Combining Multiple Model Attributes
model Order {
id Int @id @default(autoincrement())
userId Int
status Status
createdAt DateTime @default(now())
@@unique([userId, createdAt])
@@index([status])
@@index([createdAt(sort: Desc)])
@@map("orders")
@@schema("commerce")
}
| Tip | Detail |
|---|---|
| Group together | Place at end of model body |
| Index per query | Add @@index for each frequent where/orderBy |
9. Understanding Attribute Order
| Position | Convention |
|---|---|
| First | Composite keys (@@id, @@unique) |
| Middle | Indexes |
| Last | Mapping/schema (@@map, @@schema) |
10. Validating Model Attributes
| Validation | Tool |
|---|---|
| Static | prisma validate |
| Runtime | Migration engine on migrate dev |
| Common error | Duplicate map names, unknown fields |