Home > Back-end >  How do i insert values into a column within a specific row in PostgreSQL?
How do i insert values into a column within a specific row in PostgreSQL?

Time:09-23

I'm trying to insert table values into my postgreSQL database into a certain row but struggling, any ideas?

The code I've been using is the following but it keeps returning errors.For clarity, applicationname and applicationemail are the values and role_id is personal to the row.

I know the following code is wrong but it's the closest line i can think of achieving it.

Where am i going wrong?

    await pool2.query(
      "INSERT INTO contracts (applicationname, applicationemail) VALUES ($1, $2) WHERE role_id = $3",
      [name, email, role_id]
    );

*disclaimer, I'm recently new to coding any help would be much appreciated!

CodePudding user response:

First, the obvious error is syntaxis error: INSERT command does not have WHERE clause. This is the reason for the raisng error.

The second error is logical: INSERT command adds a NEW row in the table. To modify the EXSITNG row you need UPDATE command it has WHERE clause (as sad @a_horse_with_no_name)

  • Related