Hi <:vmathi:812484657547313212> Does anyone alread...
# sql
o
Hi Does anyone already worked with triggers? I tried that but as you can see, I get an error and I don't really know why :/
I'm trying to add a new "preferences" when a user signed up, with default values, that's all
s
Try running each different query one at a time and see if any throws an error (just comment the others out with
--
or [ CTRL ] + [ / ] while the lines are selected)
If I was to take a guess, I'd say you need to provide args to
VALUES()
Also, you could probably optimise this with a
create table if not exists
and removing the
drop table
. If you're trying to empty the table, you can do
truncate table public.preferences
k
You will probably also need to add Security definer to the function 🙂
o
I can't for now becaue I'm doing tests, so I need to restart everything to be sure it works correctly, but it's a good catch, thanks you for that :p
I can't, because it requires the function to work :/
A security definer?
What could I put to trigger default values?
k
Copy code
SECURITY DEFINER specifies that the function is to be executed with the privileges of the user that created it.
I stumbled upon creating a trigger that calls a function that does read some data that only creator_id = uid() can access. if you are doing such thing, then the definer basically bypass that
s
This might work:
Copy code
sql
INSERT INTO
  preferences(language, theme)
  SELECT (DEFAULT, DEFAULT)
    FROM preferences
o
Ooh okay, nice catch, i'm gonna try :p
s
You might also need to provide the
id
column as well, and perhaps pass
new.id
as the value
f
oh wow theres already a thread here
so is this for theme integration?
o
It works! Thanks you!
I really like postgres' shortcuts, didn't know that one, good to know :p
Theme integration?
f
if this is for theme integration, wouldn't it be better to handle theme persistence through localstorage to save requests?
like
o
Both
f
storing whether or not the theme is "dark mode" or "lightmode"
o
Like each user can have a preference, which is stored locally and in remote. So if you login using another computer, you don't have the light mode if you won't
s
Yeah, it's something you pick up as you learn more. If in doubt, google has my back 😄
o
You know the thing which destroy your eyes
f
ah
o
Yeah, but honestly I don't find postgres doc really intuitive
f
i just have darkmode default on my project :P
o
Like it's hard to understand in my opinion
Well I'm doing both so I don't have any issue xD (and I can dev on the beach (I don't have any beach near of me))
s
The official docs are difficult. Stackoverflow and some tutorial sites out there usually cover things much more easily
o
Yeah totally agree
f
i haven't touched the sql procedures yet, still new to that aspect, i only have a thing in there that adds users to a public users table whenever a new users signs up
o
I'm thinking about creating a serie of postgres tutorial on dev.to, could be better to get a docs
But I don't have the time yet 😭