38 Replies
i'm pretty sure you can't do line 9 for
create_data +
ok so some logs point me to the last line, I guess it does not like using create_date in calculation
yep
any ideas how to do that?
you could do a trigger to update expire_date after create
oh jeez never even heard of that before
is this postgres?
yea
the super ugly but super informative postgres docs
https://www.postgresql.org/docs/current/sql-createtrigger.html
the more friendly one
https://www.tutorialspoint.com/postgresql/postgresql_triggers.htm
PostgreSQL Documentation
CREATE TRIGGER
CREATE TRIGGER CREATE TRIGGER — define a new trigger Synopsis CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER name …
PostgreSQL - TRIGGERS
PostgreSQL - TRIGGERS, PostgreSQL Triggers are database callback functions, which are automatically performed/invoked when a specified database event occurs.
cool thx, I could also probably do this right?
yeah that would work
timestamp would def be same since its based on transaction start afaik
I'll stick with that for now and check these triggers out later
thx for the help!
super useful for certain stuff, but it's hard to test because you don't know when it will trigger
its usually fast
interesting
We have "rooms" that expire after some time
and this was best way I could think of doing the calc for it not in TS
trigger sounds really useful for this sort of thing
yeah no worries! glad to put it on your plate, it's good to know 🙂
@MrNiceRicee oh also, why is it ok for me to use duration column but not create_date
unless its not and I am about to find that out lol
postgres does not have access to create_date, when it's creating the row
oh but it does have duration since its coming from insert query?
hold on, lemme look back at it lol
oh interesting, i think it should also fail too with that
I am about to see haha
I assume it will
yeah probably a trigger will solve it
looks like I might need to do triggers
date_trunc I guess?
DEFAULT missing
oh wait yeah let me retry
cannot use column ref in default
wait I think it was working
yeah it's telling you that you can't use the
duration
column yep still broken without default
alright triggers it is
@MrNiceRicee last thing, what should I put as value of something I will update with trigger?
like just a data type of date?
sorry, when i mentioned trigger meant it in two parts
trigger -> the thing that listens on the database
function -> thing that runs
oh I see now
so here is my issue I guess
I wait on server for respone from db
the data I get back from db would need to have that expire_date to send out to event bus
something like that
since this trigger happens after that insert, how do I ensure that date happens to come back
oh so maybe I could do a before insert? i'll have to look deeper into this
or maybe its just worth doing this calculation on server
easiest would be doing the calculation on server, be easier to test and ensure you have the data there
triggers are great if you wanna do an
audit_table
yeah sounds like I would never know what expire_date would be unless I wait a bit then requery
the calculation should come out the same anyways
js Dates are just weird sometimes haha
rephrase that sentence, dates are weird in every language
in general
also since you seem to know a lot about DBS i had ONE other question lol
will postgres always use UTC unless I tell it otherwise? That is what I seem to have assumed after what I could find
also true
i'm fairly certain it's typically UTC
and also depends if you want to add timezones
then it's that timezone where the db lives -- i think, gotta double check
I think ur right
and u can set that
but I THINK timezone defaults to UTC, just wasnt sure if it auto changes ever, I think in docker it doesn't but that could be wrong and I could not find any solid answer tbh