Is there an example anyone can provide as to how t...
# help
a
Is there an example anyone can provide as to how to generate a unix timestamp of the current time for JSON? For instance, I have a default value for a column within a table set to
{"timestamp":0,"accesses":0}
but I would like to set that timestamp to the current UNIX timestamp when a row is created through a trigger
n
Hello @akito! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
t
You should be able to make the default value
Copy code
sql
SELECT json_build_object('timestamp', EXTRACT(EPOCH FROM now()), 'acesses', 0)
n
akito (2022-04-17)
a
Thank you
Copy code
SELECT json_build_object(
    'timestamp', ROUND(EXTRACT(EPOCH FROM now())), 
    'accesses', 0
) AS built_bucket_data;
INSERT INTO public.user_data (user_id, bucket_data)
  VALUES ('6cf9c0d5-eeee-eeee-eeee-d09baab689df', built_bucket_data);
I put this into the SQL editor for testing and I think this should work, I'm getting
column "built_bucket_data" does not exist
though. I'm not familiar with postgresql at all - I'm pretty sure the
INSERT
call has the right format, so maybe it's SELECT which is being weird.
I got an invalid syntax error when I attempted to set the default value to the snippet above within the Supabase dashboard, so I was going to integrate it into the function trigger instead
Finally fixed it with:
Copy code
WITH constants (built_bucket_data) as (SELECT json_build_object(
    'timestamp', ROUND(EXTRACT(EPOCH FROM now())), 
    'accesses', 0
))

INSERT INTO public.user_data (user_id, bucket_data)
  VALUES ('6cf9c0d5-eeee-eeee-eeee-d09baab689df', (SELECT built_bucket_data FROM constants));