How to add "ON UPDATE" to column definition in prisma schema? - prisma

I want to create column with the following definition
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE now(),
But when I type in schema.prisma
updated_at DateTime #updatedAt #db.DateTime(0)
Then I obtains table with column:
`updated_at` datetime NOT NULL,
How to add ON UPDATE now() to this column using prisma? I use MySQL/MariaDB.
References:
Connected problem
https://github.com/prisma/prisma/issues/5799#issuecomment-894631317
Article about ON UPDATE
https://medium.com/#bengarvey/use-an-updated-at-column-in-your-mysql-table-and-make-it-update-automatically-6bf010873e6a

Based on the client reference it seems that the client itself is responsible for providing the updated at DATETIME value instead of the way you're wanting it (to be done in the DB server).
If you really need the table/column definition to be exactly as you want, I would suggest creating or modifying the table directly in SQL and then using introspect to import the definition in your schema.
You could even add the definitions to your prisma migration files as these are just plain sql files. How to do this can be found in the Customizing Migrations article

Related

Supabase: How to automatically update a timestamp field after updating a row?

What DB I am using?
Supabase hosted version
What do I need?
After I update a row with .update({ name: 'Middle Earth' }) method I need to
automatically update also a timestamp in my table.
How can I update automatically a timestamp?
1)If you already have a table use this script (provided by Supabase devs themselves):
create extension if not exists moddatetime schema extensions;
-- assuming the table name is "todos", and a timestamp column "updated_at"
-- this trigger will set the "updated_at" column to the current timestamp for every update
create trigger handle_updated_at before update on todos
for each row execute procedure moddatetime (updated_at);
2)What if I don't wanna use the moddatetime extension?
This stackoverflow question will give you an answer.

Is it possible to update a column(automatically) with "current_timestamp" in PostgreSQL using "Generated Columns"?

Is it possible to update a column (automatically) with "current_timestamp" in PostgreSQL using "Generated Columns", whenever the row gets update?
At present, I am using trigger to update the audit field last_update_date. But I am planning to switch to generated column
ALTER TABLE test ADD COLUMN last_update_date timestamp without time zone
GENERATED ALWAYS AS (current_timestamp) STORED;
Getting error while altering column
ERROR: generation expression is not immutable
No, that won't work, for the reason specified in the error.
Functions used in generated columns must always return the same value for the same arguments, that is, depend on nothing but the current database row. current_timestamp obviously is not of that kind.
If PostgreSQL did allow such functions to be used in generated columns, then the value of the column would change if the database is restored from a pg_dump, for example.
Use a BEFORE INSERT OR UPDATE trigger for this purpose.

Created date, last modified date fields in postgress

In PostgreSQL, is there a way to add columns that will automatically record the creation date and latest updated date of a row?
for table creation date look to event triggers
for insertion look into DEFAULT value for timestamptz column (works only if you don't explicitly define value)
for last modification, use trigger FOR EACH ROW before DELETE/UPDATE
The idea - Robust way of adding created and modified fields for data we add to database through db triggers
Update modified_by and modeified_on or modified_at for every db transaction.
Pick created_on and created_by or created_at from modified details whenever you insert a row into tables.
For trigger function, check this repo https://github.com/charan4ks/created_fields.git

Scala Slick auto update a column when the row is updated

I want add a updated_at column for every table.
The column will update its value with current time when the row is updated.
How can I automatic update it's value to avoid writing verbose code?
Thanks
You can do that directly in the database, don't need to modify your Slick code. you can define it like this, then it will automatically update whenever any value of this row is modified:
alter table xx add column updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
(this works mysql, not sure if needs adjustment for other databases)

Postgresql internal record id / creation date

I'm trying to determine whether or not postgresql keeps internal (but accessible via a query) sequential record ids and / or record creation dates.
In the past I have created a serial id field and a record creation date field, but I have been asked to see if Postgres already does that. I have not found any indication that it does, but I might be overlooking something.
I'm currently using Postgresql 9.5, but I would be interested in knowing if that data is kept in any version.
Any help is appreciated.
Thanks.
No is the short answer.
There is no automatic timestamp for rows in PostgreSQL.
You could create the table with a timestamp with a default.
create table foo (
foo_id serial not null unique
, created_timestamp timestamp not null
default current_timestamp
) without oids;
So
insert into foo values (1);
Gives us
You could also have a modified_timestamp column, which you could
update with an after update trigger.
Hope this helps