title: How to avoid inserting null values in postgres database
body: <p>I have the following query that inserts data into a many-to-many join table</p>
  
  <pre><code>INSERT INTO playlist_genre(playlist_id, genre_id)
  VALUES  (${playlistId}, ${genre1Id}),
          (${playlistId}, ${genre2Id}),
          (${playlistId}, ${genre3Id})
  );
  </code></pre>
  
  <p>However the problem I've come to is that the values <code>genre2Id</code> and <code>genre3Id</code> are not required by the user so could either be an <code>INTEGER</code> or <code>NULL</code>.</p>
  
  <p>I am trying to find a way to write this same query but so it only inserts if a value is present. Both columns have <code>NOT NULL</code> constraints.</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Here is my Playlist class</p>
  
  <pre><code>class Playlist {
    constructor(playlist) {
      // required fields
      this.title = playlist.title;
      this.playlistType = playlist.playlistType;
      this.userId = playlist.userId;
      this.numberOfTracks = playlist.numberOfTracks;
      this.lengthInSeconds = playlist.lengthInSeconds;
      this.genre1Id = playlist.genre1Id;
      // not required fields
      this.genre2Id = playlist.genre2Id || null;
      this.genre3Id = playlist.genre3Id || null;
      this.description = playlist.description || null;
      this.releaseDate = playlist.releaseDate || null;
    }
  
    save() {
      return new Promise((resolve, reject) =&gt; {
        db.one(sqlCreatePlaylist, this)
          .then((insertedPlaylist) =&gt; {
            // Here is where i want to use insertedPlaylist's id 
            // to insert data into 'playlist_genre' table
            resolve(insertedPlaylist);
          })
          .catch(error =&gt; reject(error));
      });
    }
  </code></pre>
  
  <p>Here is what sqlCreatePlaylist looks like</p>
  
  <pre><code>INSERT INTO playlists(
    user_id,
    title,
    playlist_type,
    number_of_tracks,
    duration,
    description,
    release_date
  )
  VALUES(
    ${userId},
    ${title},
    ${playlistType},
    ${numberOfTracks},
    ${lengthInSeconds},
    ${description},
    ${releaseDate}
  )
  RETURNING *;
  </code></pre>