0

I get the error 42601 when using postgresql

I created the table using pgAdmin. The autogenerated code looks as follows.

-- Table: posts
-- DROP TABLE posts;

CREATE TABLE posts
(
  post_id bigserial NOT NULL,
  title character varying(150) NOT NULL,
  description character varying(500),
  posted_at timestamp with time zone,
  last_edited timestamp with time zone,
  "user" character varying(50) NOT NULL,
  editor character varying(50),
  up_votes integer NOT NULL,
  down_votes integer NOT NULL,
  flag character varying(7),
  CONSTRAINT "PK_post_id" PRIMARY KEY (post_id),
  CONSTRAINT "FK_user" FOREIGN KEY ("user")
      REFERENCES users (login) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE posts
  OWNER TO postgres;

I execute an insert operation with the following helper method.

  addPost(Map values){
    connect(uri)
    .then((conn){
      conn.execute('insert into posts values(@title, @description, now(), now(), @user, @editor, @upVotes, @downVotes, @flag', values)
      .then((_) => conn.close())
      .catchError((err){
        print('Execute error in addPost: $err');
      })
      .whenComplete(() => conn.close());
    })
    .catchError((err) => print('Error in addPost: $err'));
  }

where Map values has the form:

 {'title': 'Sample title', 'description': 'This is a description for a sample post', 'user': 'dartUser', 'editor': 'dartUser', 'upVotes': 0, 'downVotes': 0, 'flag': 'healthy'}

I'm not a postgresql expert, so this might be something trivial I just don't see.

1 Answer 1

1

You can find the error codes for postgresql here. Error 42601 means a syntax error.

I suspect the cause is that you are missing a closing bracket on the 'values' clause, after @flag.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. Also it seems that user is a bad choice of column name. For future reference. If you want this to actually work you need to execute something like this: conn.execute('insert into posts(title, description, posted_at, last_edited, "user", editor, up_votes, down_votes, flag) values(@title, @description, now(), now(), @user, @editor, @upVotes, @downVotes, @flag)', values)

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.