last day (14 days later) » 

11:30 AM
0
A: add column only if table not exists

Vao Tsunyour code ALTER TABLE mytable IF NOT EXISTS ADD theField text will add column thefield. if you want it to be called precisely theField you should run this: ALTER TABLE mytable IF NOT EXISTS ADD "theField" text This is why I check if column exists ignoring the case: lower(blaH-BLah) equal to lower...

 
What is myschema?
 
schema name where you have your table
if you did not specify it anywhere - should be public
 
:I am receiving : There was an error : { error: missing "THEN" at end of SQL expression . I uploaded here.Note,that i used ' ' outside in order to use the theField.and I ommited the lower(..).
 
you cant ommit column part if you want to check if column exists. also you cant use "' for text value, dbl quotas are used for object names. in this case theField etc is not a name - it is part of where clause
 
I want to keep the theField with lower and uppercase letters.Is the <1 you updated does that?
 
11:31 AM
no - it checks if number of columns with such name in such table is less then one, then it adds it
 
hello
 
hi
 
First of all we must remve the ")" from
" and table_name = 'mytable' )"+
because it was left when I ommited the lowercase
you said that
" and lower(column_name) = lower('theField')) < 1 " +
 
yeah - copied your code
to replace " with '
 
is mandatory?I can't understand why I should have it.
 
11:33 AM
try running now
 
And what if I have thefield as lowercase already?
 
dbl quotes are used for object names
when you refer to them
not when you mention
this is why you use lower() two times
it makes check case insencitive
 
Oh , ok! So , when I want to use theFields I will just put it in the
"ALTER TABLE mytable ADD 'theField' text;"+
 
yes
 
but , I will leave it as theField ? with single quotes?
 
11:35 AM
and this
" and lower(column_name) = lower('theField')) < 1 " +
becomes
 
And if I want to use thefield , again with single quites?
 
" and column_name = 'thefield') < 1 " +
becuse you know exactly you dont have mixec case in column name
 
I wanto keep the lower/uppercase because I am using this in other files as reference
 
then you should change your previous code
to use mixed case in postgres object name, you should ALWAYS surrond the name with double quotes
 
That's what I did in the code I posted to you
 
11:37 AM
otherwise the name will be lowercase
 
yes,but then I have to surround the whole expression with single quotes
 
ALTER TABLE mytable ADD 'theField' text; wont work
 
as the code I uploaded
 
ALTER TABLE mytable ADD theField text; is equal to ALTER TABLE mytable ADD thefield text;
so if you want it with mixed case use ALTER TABLE mytable ADD "theField" text;
if not, just ALTER TABLE mytable ADD theField text; - it will automatically became thefield, ignoring your case
 
Yes,ok with this.But in javascript where I execute my query I have to use single outer quotes and inside them use the douvble. like : client.query( ' ALTER TABLE mytable ADD "theField" text ')
 
11:41 AM
I guess I see
I changed the code in answer
this is what you meant?.
"ALTER TABLE mytable ADD \"theField\" text;"+ this part
 
You used \"theField"\
 
to create column in mixed case
 
So , I don't need to use single quotes now, right?
 
no
 
I didn't know that . I will try it and let you know!
 
11:42 AM
singli quotes you use when you work with strings, not with relations
in JS, you can use +'ALTER TABLE mytable ADD "theField" text;'+ to avoid escape backspaces
but better use same quotes for the whole statement, just to be more clear
 
ok
Nice!Thanks! It works fine! upvoted
 
cool
cheers!
 

last day (14 days later) »