Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use database triggers instead of engine-level code for @updatedAt #6772

Open
AlaaZorkane opened this issue Apr 25, 2021 · 3 comments
Open

Use database triggers instead of engine-level code for @updatedAt #6772

AlaaZorkane opened this issue Apr 25, 2021 · 3 comments
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: database triggers topic: schema topic: updatedAt

Comments

@AlaaZorkane
Copy link

AlaaZorkane commented Apr 25, 2021

Problem

Would be nice if while migrating, prisma generates a trigger for @updatedAt fields, this way if accessing the db with something other than prisma, we get the same behavior.

Suggested solution

From this

model User {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now()) @updatedAt

  email    String    @unique
  name     String?
}

to this

-- CreateTable
CREATE TABLE "User" (
    "id" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "email" TEXT NOT NULL,
    "name" TEXT,

    PRIMARY KEY ("id")
);
-- This part! Also, this could be a function
CREATE EXTENSION IF NOT EXISTS moddatetime;
CREATE TRIGGER update_timestamp
    BEFORE UPDATE ON public."User"
    FOR EACH ROW
    EXECUTE PROCEDURE moddatetime("updatedAt");

Note that this solution is for pgsql only!

But triggers are available in sqlite/mysql/postgresql

IF for any reasons something like moddatetime doesn't exist in other databases (mysql/sqlite) then we can resort to the engine-level updatedAt

Alternatives

Keep using @updatedAt and manually adding the triggers by yourself to each table created by prisma.

Additional context

Wanted to add records from another tool (graphile) and found out that I needed the updatedAt field to be updated on the database level.

@AlaaZorkane AlaaZorkane changed the title Prisma migrate: use database triggers instead of app code for @updatedAt Prisma migrate: use database triggers instead of engine-level code for @updatedAt Apr 25, 2021
@AlaaZorkane
Copy link
Author

Good to mention that there is some debating ground on this one! Per example from sequelize docs:

image
Direct link

I honestly can't see a reason why a trigger would be bad in this context (updatedAt field) but would love to know more about the motives to keep this internal to the engine itself.

@pantharshit00 pantharshit00 added the domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. label Apr 29, 2021
@janpio janpio added kind/feature A request for a new feature. and removed kind/feedback Issue for gathering feedback. labels Jun 14, 2021
@janpio janpio changed the title Prisma migrate: use database triggers instead of engine-level code for @updatedAt Use database triggers instead of engine-level code for @updatedAt Jun 14, 2021
@janpio
Copy link
Member

janpio commented Jun 14, 2021

This could be an additional feature of the Prisma Schema to be able to define that @updatedAt should use database level triggers instead of Client side timestamp generation. (We are highly unlikely to change the existing implementation in Prisma as many people rely on it.)

@AThilenius
Copy link

We have several systems (Rails, Hasura and Prisma/Node) accessing a shared PSQL DB, and triggers are the only reliable way to set the updated_at field. I would love a way to define this in the schema.prisma file!

Even better (from a maintenance standpoint) would be support for custom attributes, and a community-build set of plugins for things like this. I would happily throw some time at that.

Related:
#1660
#4002

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: database triggers topic: schema topic: updatedAt
Projects
None yet
Development

No branches or pull requests

4 participants