Using Database Functions

1. Using Auto-Increment

AspectDetail
Schema@default(autoincrement())
PGUses SERIAL/IDENTITY
MySQLAUTO_INCREMENT

2. Generating UUIDs

StrategyDetail
Prisma side@default(uuid()), uuid(7)
DB side@default(dbgenerated("gen_random_uuid()")) (PG)

3. Using CUID Generation

FunctionDetail
cuid()v1, 25 chars
cuid(2)Shorter, secure, URL-safe

4. Setting Current Timestamp

SourceSyntax
Prisma@default(now())
DB@default(dbgenerated("CURRENT_TIMESTAMP"))

5. Using Database-Generated Values

code String @default(dbgenerated("substr(md5(random()::text), 1, 8)"))
UseDetail
Custom expressionsAny SQL expression returning the field type
Computed columnsGenerated columns via raw SQL

6. Implementing Custom SQL Functions

PatternDetail
Create functionVia migration SQL
CallVia raw query

7. Using Sequence Functions

order_no Int @default(dbgenerated("nextval('order_no_seq')"))
DBSupport
PGNative sequences
CockroachDBsequence()

8. Applying String Functions

FunctionUse
LOWER/UPPERCase normalize
TRIMWhitespace cleanup
SUBSTRExtract substring

9. Using Mathematical Functions

FunctionUse
ROUND/FLOOR/CEILNumeric rounding
ABSAbsolute value
POWER/SQRTExponential

10. Handling NULL with Coalesce

SELECT COALESCE("name", 'Anonymous') AS display_name FROM "User";
FunctionUse
COALESCEFirst non-null
NULLIFReturn null if equal
IFNULLMySQL equivalent