Jienan  Tang
Jienan Tang

Reputation: 29

pg-promise creating column error

var name = req.body.name;
db.any('alter table "houseList" add  $1 text', [name])

I tried to add a new column to database host on heroku using the above code in nodejs but I keep getting this error :

error: syntax error at or near "'haha'"

'haha' is the value inside name, anyone have any idea what is wrong?

Upvotes: 2

Views: 427

Answers (1)

vitaly-t
vitaly-t

Reputation: 25840

You are using invalid escaping for the column name, as a regular string variable.

Any schema/table/column name are referred to as SQL Names, and must be escaped using "".

Within pg-promise that means you must use its SQL Names support, with :name: or ~ modifier.

db.any('alter table "houseList" add  $1:name text', [name])

or

db.any('alter table "houseList" add  $1~ text', [name])

Also, if you sure that you are only using simple names, i.e. no white spaces, no capitals, then you can use the name directly, unescaped, which means using the Raw Text, via modifier :raw or ^. But generally, it is not recommended, i.e. escaping the names is recommended as safer ;)

Upvotes: 2

Related Questions