-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
[FEATURE]: Prisma's updatedAt
behavior. onUpdate
API suggestion
#956
Comments
What is the current approach to implementing updatedAt? |
@zivtamary either manually when updating records by providing new value for |
Related: #843 |
Would really be interested by this feature ! |
You can ask the database to update the timestamp for you
Or, you can do it in your application code
Right now Drizzle is a lightweight ORM with no learning curve. You read through the docs in no time. In Prisma docs, you can get lost for days, and SQL knowledge won't help you much. I hope Drizzle will stay like this and will leave application code for developers to implement in their apps. |
I would love to use the above approach using the For me, the benefit of defining triggers in the |
@Pentiado That does not work in postgres either, looks like only mysql knows this |
This can now be achieved with the new |
@Angelelz How it's related to the |
Nope, I was mistaken. Based on the shape of the API in the initial feature request, it looked a lot like |
@Angelelz Exactly. |
any updates on this? I saw a PR pending merge |
For PostgreSQL, until this is implemented, this is what I'm using for the time being: create or replace function on_row_update_set_updated_at() returns trigger
language plpgsql as
$$
begin
NEW."updated_at" = now();
return NEW;
end;
$$;
create or replace function on_create_table_add_trigger_on_row_update_set_updated_at() returns event_trigger
language plpgsql as
$$
declare
obj record;
tbl_name text;
begin
for obj in select * from pg_event_trigger_ddl_commands() where object_type = 'table'
loop
tbl_name := obj.objid::regclass;
if exists(select 1
from information_schema.columns
where table_schema = obj.schema_name
and table_name = tbl_name
and column_name = 'updated_at') then
execute format(
'create or replace trigger on_row_update_set_updated_at before update on %I for each row execute procedure on_row_update_set_updated_at();',
tbl_name);
end if;
end loop;
end
$$;
create event trigger
on_create_table_add_trigger_on_row_update_set_updated_at on ddl_command_end
when tag in ('CREATE TABLE', 'CREATE TABLE AS')
execute procedure on_create_table_add_trigger_on_row_update_set_updated_at(); Create your tables with "updated_at" timestamp with time zone DEFAULT now() NOT NULL column, and it'll be updated automatically whenever any row changes. |
@AlvaroAquijeDiaz
What is that PR? I did a quick search and couldn't find it. I might open one if no one else has one open. |
We would also love to have this in drizzle. Currently, this is not really possible in a meaningful way with drizzle. |
This is a core schema creation feature, at least for MySQL. Why is this labelled as an enhancement? |
Postgres doesn't have an onUpdate. We're looking into this one. We're thinking that an API like the |
@Angelelz understood, thank you. I checked out the linked PR. A concern that I have is that implementing this functionality at the library level would be counter-productive for the MySQL dialect which has native support at the schema level for the trigger. Could we possibly implement this in such a way that this utilizes the schema definition for MySQL whereas use the lib's internal implementation or some custom plpgsql method for pg and other dialects? |
How would it be counterproductive for MySql? We already have a similar feature with the |
I'm saying that we should be replicating the behaviour described in this reference manual specifically for the |
This is implemented already in mysql. We have in MySql the method updateQty: int("updateQty").$onUpdate(() => sql`updateQty + 1`) Now I know this can be done with a generated column in MySql, but not the datetime stuff in Pg, and also this is opt-in behavior. |
I had no idea about the |
It is not. Can you raise a documentation issue? |
@Angelelz according to the docs there's no |
I was referring to a feature being proposed, not a current feature. |
The functionality that developers can extend their drizzle client like prisma client extension. Scenarios that I have faced:
|
any news on this issue? I am using postgres and currently there is not a drizzle-integrated way to handle updated_at field. |
You can subscribe to this issue and the PR #1509 |
@givenloyiso It's currently possible only for MySql drivers, not for any others |
Hmm, I don't seem to be able to use export const createTable = mysqlTableCreator((name) => `asdf_${name}`);
export const users = createTable(
"user",
{
id: varchar("id", { length: 256 }).primaryKey(),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: datetime("updatedAt").onUpdateNow(),
},
(table) => ({}),
); I get this error on I also see nothing in the documentation for Drizzle that even mentions UPDATE: After doing more googling it looks like it's a PlanetScale issue that prevents onUpdateNow(). Boo. Wish the docs explained this stuff. |
This is what is needed updatedAt: timestamp('updated_at', { mode: 'date', precision: 3 }).$onUpdate(() => new Date()), |
You probably do not want to use
|
This would cause issues with stuff like drizzle-graphql since it assumes its a date, is there another way do use Date with db server time? |
I dont know the reason, but when I tried to update an entry I received this error |
I am getting |
It works with postgres-js library and PostgreSQL database export const users = pgTable('users', {
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at')
.defaultNow()
.$onUpdate(() => new Date()),
}); However, with $onUpdate(() => sql`CURRENT_TIMESTAMP`), I got an error that was mentioned by others: Any solution how to use |
@belkocik workaround here: #2212 (comment) |
My setup. It works flawlessly
|
Describe what you want
Prisma has awesome feature such as declaring a column with
@updatedAt
which updates that column with a new timestamp every time the record updates.Instead of suggesting to add something similar like just
updatedAt
alternative, what if you provide a more flexibleonUpdate
method when declaring columns in schema so we can do something like this:The idea is that this method accepts a callback function that returns a new value for the column. The callback function is invoked every time the record updates. The basic example is to use it for timestamps on
updatedAt
columns, but it can be used for anything since the new value will be the one returned by the callback.In general, when the record updates, we can find out which columns were updated, old values and new values. Maybe, this data can be accepted by
onUpdate
callback likeOr any other better API for this.
The text was updated successfully, but these errors were encountered: