1

I'm trying to switch my psql setup over to node, and can't get the following test query to work.

PG = require('pg')

module.exports = class Postgres extends Backbone.Model
    initialize: =>
        PG.connect process.env.DATABASE_URL, (error, client) =>
            this.client = client

            this.sendMessage(1, 2, '3')


    sendMessage: (from, to, message) =>
        this.client.query('INSERT INTO messages(from, to, content) VALUES($1, $2, $3) RETURNING id', [from, to, message], (error, result) =>
            console.log 'error', error
            console.log 'result', result
        )

Responds with the following error:

 error { [error: syntax error at or near "from"]

What am I doing wrong?

I don't know if it matters, but here's my table. enter image description here

3
  • Sorry for the coffeescript btw. Commented Jul 22, 2015 at 7:21
  • Of course! Man, I am the absolute worst. Thanks Adriano Commented Jul 22, 2015 at 7:29
  • Converted my commented into an answer, so people can spot it more easily.. Cheers, August! Commented Jul 22, 2015 at 7:32

1 Answer 1

2

I think Postgres is complaining about the reserved keyword from as a column identifier.

You should try double-quoting it in your query like this:

INSERT INTO messages("from", to, content) [...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.