title: How to make constraint different for id
body: <p>In my ReactJS app I'm fetching information from my NodeJS app to display on the page the album by id. When I add songs to my database it has to go through the POST request and add the info to the database but the database has a constraint where the index key has to be unique. What I'm trying to do in the code below is use Promises to select album by id then insert the data on that album_id so that the insert and constraint is only ran on that id. This doesn't work and the constraint is still ran on all objects the same. Does anyone have a solution as on what I could do in order to run the constraint separately so that different albums can have the same index?</p>
  
  <pre><code>  const addSong = (request, response) =&gt; {
    const id = parseInt(request.params.id)
     const { name, link, index, album_id } = request.body;
  
    let p = new Promise((resolve, reject) =&gt; {
      if(true) {
        resolve('Success')
      } else {
        reject('Failed')
      }
    })
  
    for (let i = 0; i &lt; request.body.length; i++) {
      const object = request.body[i]
      p.then(() =&gt; {
         pool.query('SELECT * FROM songs WHERE album_id = $1', [album_id], (error, results) =&gt; {
         if (error) {
          throw error
        } else {
         console.log("SELECT " + JSON.stringify(request.body));
      }
      })
      })
  
        .then(() =&gt; {
          pool.query(
            'INSERT INTO songs (name, link, index, album_id) VALUES ($1, $2, $3, $4) ON CONFLICT (index) DO NOTHING RETURNING *',
            [
              object.name,
              object.link,
              object.index,
              object.album_id,
            ],
            (error, results) =&gt; {
              if (error != null) {
                console.log(error)
              } else {
                console.log('INSERT ' + JSON.stringify(request.body))
              }
            }
          )
        })
        .catch(err =&gt; {
          console.log(err)
        })
    }
  }
  </code></pre>