Using Database Functions
1. Using Auto-Increment
| Aspect | Detail |
| Schema | @default(autoincrement()) |
| PG | Uses SERIAL/IDENTITY |
| MySQL | AUTO_INCREMENT |
2. Generating UUIDs
| Strategy | Detail |
| Prisma side | @default(uuid()), uuid(7) |
| DB side | @default(dbgenerated("gen_random_uuid()")) (PG) |
3. Using CUID Generation
| Function | Detail |
cuid() | v1, 25 chars |
cuid(2) | Shorter, secure, URL-safe |
4. Setting Current Timestamp
| Source | Syntax |
| Prisma | @default(now()) |
| DB | @default(dbgenerated("CURRENT_TIMESTAMP")) |
5. Using Database-Generated Values
code String @default(dbgenerated("substr(md5(random()::text), 1, 8)"))
| Use | Detail |
| Custom expressions | Any SQL expression returning the field type |
| Computed columns | Generated columns via raw SQL |
6. Implementing Custom SQL Functions
| Pattern | Detail |
| Create function | Via migration SQL |
| Call | Via raw query |
7. Using Sequence Functions
order_no Int @default(dbgenerated("nextval('order_no_seq')"))
| DB | Support |
| PG | Native sequences |
| CockroachDB | sequence() |
8. Applying String Functions
| Function | Use |
LOWER/UPPER | Case normalize |
TRIM | Whitespace cleanup |
SUBSTR | Extract substring |
9. Using Mathematical Functions
| Function | Use |
ROUND/FLOOR/CEIL | Numeric rounding |
ABS | Absolute value |
POWER/SQRT | Exponential |
10. Handling NULL with Coalesce
SELECT COALESCE("name", 'Anonymous') AS display_name FROM "User";
| Function | Use |
COALESCE | First non-null |
NULLIF | Return null if equal |
IFNULL | MySQL equivalent |