title: Android Room persistence SQLiteConstraintException when inserting items twice
body: <p>I have been working with the Room persistence library which worked great until I items in two different insertions.</p>
  
  <p><strong>Sound Entity class</strong></p>
  
  <pre><code>@Entity
  public class Sound {
  @PrimaryKey(autoGenerate = true)
  private int sid;
  @ColumnInfo(name = "sound_theme")
  private int soundTheme;
  @ColumnInfo(name = "sound_name")
  private String soundName;
  @ColumnInfo(name = "sound_resource")
  private int soundResource;
  @ColumnInfo(name = "sound_image_resource")
  private int soundImageResource;
  @ColumnInfo(name = "sound_favorite")
  private boolean isFavorite;
  @ColumnInfo
  private int usedCount;
  @ColumnInfo
  private long lastUsed;
  
  
   public Sound(int soundTheme, String soundName, int soundResource, int soundImageResource, boolean isFavorite, int usedCount, long lastUsed) {
      this.soundTheme = soundTheme;
      this.soundName = soundName;
      this.soundResource = soundResource;
      this.soundImageResource = soundImageResource;
      this.isFavorite = isFavorite;
      this.usedCount = usedCount;
      this.lastUsed = lastUsed;
  }
  </code></pre>
  
  <p>Watch that I have a sound id as a primary key which auto-generates.</p>
  
  <p><strong>Sound DAO</strong> </p>
  
  <pre><code>@Dao
  public interface SoundDao {
  
  @Query("SELECT * FROM sound")
  List&lt;Sound&gt; getAll();
  
  @Query("SELECT * FROM sound WHERE sid IN (:soundIds)")
  List&lt;Sound&gt; loadAllByIds(int[] soundIds);
  
  @Query("SELECT * FROM sound WHERE sound_name LIKE :name LIMIT 1")
  Sound findByName(String name);
  
  @Query("SELECT * FROM sound WHERE sid LIKE :id LIMIT 1")
  Sound findById(int id);
  
  @Insert
  void insertAll(Sound... sounds);
  
  @Insert
  void insertAllArrList(ArrayList&lt;Sound&gt; sounds);
  
  @Delete
  void delete(Sound sound);
  
  @Query("DELETE FROM sound")
  public void deleteAllSounds();
  }
  </code></pre>
  
  <p>The error goes with this line: </p>
  
  <pre><code>      if (focusedSound.isFavorite()) 
        favorites.soundDao().insertAll(focusedSound); 
        else favorites.soundDao().delete(selectedSound);
  </code></pre>
  
  <p>first insertion goes well(If you insert multiple items in this it will also work), second time you insert it crashes.
  I assume it has something to do with the primary key for the sound id(sid) but I am not sure how to solve it.</p>
  
  <p>here are some of the error logs:</p>
  
  <pre><code>06-03 20:03:36.454 8807-8807/com.zaid.green.soundpress E/InputEventReceiver: 
  Exception dispatching input event.
  06-03 20:03:36.454 8807-8807/com.zaid.green.soundpress E/MessageQueue-JNI: 
  Exception in MessageQueue callback: handleReceiveCallback
  06-03 20:03:36.460 8807-8807/com.zaid.green.soundpress E/MessageQueue-JNI: 
  android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: 
  Sound.sid (code 1555)
  SQLiteConnection at nativeExecuteForLastInsertedRowId(Native Method)  
  SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
      at 
  </code></pre>