SQL error

Anyone see an error here?? I am going crazy... thx
38 Replies
mrnicericee
mrnicericee•2y ago
i'm pretty sure you can't do line 9 for create_data +
Perfect
Perfect•2y ago
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?
mrnicericee
mrnicericee•2y ago
you could do a trigger to update expire_date after create
Perfect
Perfect•2y ago
oh jeez never even heard of that before
mrnicericee
mrnicericee•2y ago
is this postgres?
Perfect
Perfect•2y ago
yea
mrnicericee
mrnicericee•2y ago
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.
Perfect
Perfect•2y ago
cool thx, I could also probably do this right?
mrnicericee
mrnicericee•2y ago
yeah that would work
Perfect
Perfect•2y ago
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!
mrnicericee
mrnicericee•2y ago
super useful for certain stuff, but it's hard to test because you don't know when it will trigger its usually fast
Perfect
Perfect•2y ago
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
mrnicericee
mrnicericee•2y ago
yeah no worries! glad to put it on your plate, it's good to know 🙂
Perfect
Perfect•2y ago
@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
mrnicericee
mrnicericee•2y ago
postgres does not have access to create_date, when it's creating the row
Perfect
Perfect•2y ago
oh but it does have duration since its coming from insert query?
mrnicericee
mrnicericee•2y ago
hold on, lemme look back at it lol oh interesting, i think it should also fail too with that
Perfect
Perfect•2y ago
I am about to see haha I assume it will
mrnicericee
mrnicericee•2y ago
yeah probably a trigger will solve it
Perfect
Perfect•2y ago
looks like I might need to do triggers
Perfect
Perfect•2y ago
Perfect
Perfect•2y ago
date_trunc I guess?
mrnicericee
mrnicericee•2y ago
DEFAULT missing
Perfect
Perfect•2y ago
oh wait yeah let me retry
Perfect
Perfect•2y ago
Perfect
Perfect•2y ago
cannot use column ref in default wait I think it was working
mrnicericee
mrnicericee•2y ago
yeah it's telling you that you can't use the duration column crys
Perfect
Perfect•2y ago
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?
mrnicericee
mrnicericee•2y ago
sorry, when i mentioned trigger meant it in two parts trigger -> the thing that listens on the database function -> thing that runs
Perfect
Perfect•2y ago
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
mrnicericee
mrnicericee•2y ago
--- function
CREATE OR REPLACE FUNCTION add_room_expiration_func() RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
[this where you write that update]

END;
$$
-------- trigger
CREATE TRIGGER add_room_expiration_trigger AFTER INSERT ON "rooms" FOR EACH ROW EXECUTE PROCEDURE add_room_expiration_func();
--- function
CREATE OR REPLACE FUNCTION add_room_expiration_func() RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
[this where you write that update]

END;
$$
-------- trigger
CREATE TRIGGER add_room_expiration_trigger AFTER INSERT ON "rooms" FOR EACH ROW EXECUTE PROCEDURE add_room_expiration_func();
something like that
Perfect
Perfect•2y ago
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
mrnicericee
mrnicericee•2y ago
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
Perfect
Perfect•2y ago
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
mrnicericee
mrnicericee•2y ago
rephrase that sentence, dates are weird in every language in general
Perfect
Perfect•2y ago
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
mrnicericee
mrnicericee•2y ago
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
Perfect
Perfect•2y ago
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
Want results from more Discord servers?
Add your server