Search code examples
databasepostgresqltriggersormprisma

add triggers manually inside my migrations with prisma ORM


I am trying to create triggers using postgresql with prisma as ORM but it seems that it haven't supported triggers yet. and I found this answer ,but i don't want to create my triggers logic in the server level (using middlewares).

now I am asking, can i make my triggers manually inside my database migrations as a migration level, or it may cause some problems?

for example i have the following migrations inside my application

├── prisma
│   ├── migrations
│   │   ├── 20230202011931_initial
│   │   │   └── migration.sql
│   │   ├── 20230202012555_default_to_created_at
│   │   │   └── migration.sql
│   │   ├── 20230202130457_mapping
│   │   │   └── migration.sql
│   │   └── migration_lock.toml
│   └── schema.prisma

can i choose one of these files to add my triggers inside or just create another file and add my triggers inside it?


Solution

  • Yes, you can declare database triggers that are a part of migrations. You can create a migration with --create-only flag.

    So invoking the command npx prisma migrate dev --create-only will create an empty migration file. You can then add the database trigger in this newly created file. Invoking npx prisma migrate dev again would then apply this new migration.

    You can read more about customizing migrations here.