-
-
Notifications
You must be signed in to change notification settings - Fork 711
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
Feat: add an $onUpdate
method to columns
#1509
Conversation
Any progress here? |
need this feature... |
Any progress with this? |
Looks like this is working for MySQL: export const users = mysqlTable("users", {
...
updatedAt: datetime("updatedAt").default(sql`CURRENT_TIMESTAMP ON UPDATE SET CURRENT_TIMESTAMP`),
}); |
Please release this 🙏 |
Is there anything we can do to unblock this feature? |
2431a1c
to
fa3bd5e
Compare
Is there a postgres workaround |
Yes, the Postgres way of dealing with this is with stored procedures |
[Postgres] Anyone looking for an workaround until this is merged, run this on your DB - CREATE OR REPLACE FUNCTION trigger_set_timestamp ()
RETURNS TRIGGER
AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$
LANGUAGE plpgsql; |
Is there an update on this? |
For anyone else that found this didn't work, I got it working on Planetscale/MySQL by removing "SET" from the string: updatedAt: datetime("updated_at").default(sql`CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`) |
Any progress on this? Would be a super helpful feature! |
what is blocking the release of this feature? |
Actually been waiting on this since December. @dankochetov please have another look at this :) |
@dankochetov @AndriiSherman could review this PR please? |
Does anyone have a temporal approach that works in postgres, while this PR is not merged? |
@predragnikolic I saw that but I had zero clue on how to implement that in drizzle. I want to keep it versioned in a migration but I had no idea how to do it |
Is there any status on this? Have been waiting for this feature |
Folks, Drizzle is making a lot of noise and it has great potential, but not receiving a single comment or acknowledgement from a teammate on a PR open for 5 months does not look good. |
@AndriiSherman , @dankochetov can you. please take a look at this? Thanks! |
There is a subscribe button, please use it. I am sure that a lot of people are subscribed and waiting for an official update from drizzle devs. It doesn't help flooding this thread(and people email inbox) with messages like above. Thank you! |
Will try to release today |
} | ||
|
||
/** | ||
* Alias for {@link $defaultFn}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo here^
* Alias for {@link $defaultFn}. | |
* Alias for {@link $onUpdateFn}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I'll fix before release
This was released today. I feel like there's a lot of limitations to this as is, which greatly affects the potential use cases possible. a) It would be nice to have the current updating record/item as a possible context to pass to the update function, so you can use values from other fields to update the field. I guess you could do a select query inside, but that seems unnecessary if the current updating item is right there. b) It does not seem possible to use an async function to return the new value? Use case: Creating a hash value based on the value of other fields for example using sha256 using web cryptos |
@houmark yeah, that makes sense |
Also after having thought about it a bit more, I think this method is either badly named or is being utilized for more than it should. From the description of this PR:
At this point this method is no longer "on update" but also "on insert" in special cases. But And okay, if it is for both, then it should be named accordingly because the current name is very explicit "onUpdate", but it's not "just" on update. Either that, or I misunderstood the description/docs (I did not have a chance to test this out yet - mainly because this method seem to be really worth using for +90% use cases I have). |
@houmark seems like we should have |
That could be one option yes, and I like that idea as it covers every use case with no overlap, but $onUpdate should not be doing any insert related stuff under any circumstances imho. If people want to do that, then either use both functions or the upsert one. For me it's still way more limiting that the things mentioned in my first comments are not possible. This forces me to do JS logic stuff on every update prior to updating and that can become annoying if you have many of those in different places but you want 1-2 fields to always end up with a specific value - for example a hash based on other field values or something similar. |
I tried
If anyone has figured out how to use EDIT: If you set the updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'string' })
.notNull()
.defaultNow()
.$onUpdate(() => sql`current_timestamp`), |
@tanishqmanuja it works and works as expected, I don't see a reason for you to use it here |
Ah got it, thanks @AlexBlokh. But its weird that drizzle-kit studio doesn't trigger these $onUpdate functions. drizzle-studio-onupdate.mp4 |
I'm not sure we've implemented that yet, please created that as a separate issue, we'll gladly fix! |
What should be used here ? |
This will close #956. Also related to #843.
Upon merging this PR, the user will now be able to register a function that returns the value to be inserted automatically to the column on
UPDATE
queries.This will work very similar to
$default()
/$defaultFn()
but it would work for updates.Please keep in mind that by extension, if you register a function in this method, this function will also be used for inserts only if no
.default()
or.$default()
were registered.Usage
the
updatedAt
column will automatically be updated each time the row is updated.