title: Android Room FOREIGN KEY constraint failed
body: <p>I'm trying to design and implement a folder tree structure in Android SQLite with help of Android Room Persistence (an ORM) which Google introduced in I/O 2017.
  In my design, a folder can contain another folders and files.
  Here are my codes for folder and file:</p>
  
  <p>File Model:</p>
  
  <pre><code>@Entity(tableName = "files", foreignKeys = @ForeignKey(entity = Folder.class,
      parentColumns = "id",
      childColumns = "parent_id",
      onDelete = CASCADE))
  public class File {
      @PrimaryKey(autoGenerate = true)
      private int id;
  
      private String title;
      private Date creationDate;
  
      @ColumnInfo(name = "parent_id")
      public int parentId;
  
      //here setters and getters skipped but exist in original code
  }
  </code></pre>
  
  <p>And Here is Folder Code:</p>
  
  <pre><code>@Entity(tableName = "folders", foreignKeys = @ForeignKey(entity = Folder.class,
      parentColumns = "id",
      childColumns = "parent_id",
      onDelete = CASCADE,onUpdate = SET_NULL))
  public class Folder {
      @PrimaryKey(autoGenerate = true)
      private int id;
  
      private String name;
  
      @ColumnInfo(name = "parent_id")
      private int parentId;
  }
  </code></pre>
  
  <p>Which have a foreign key on ID column of it self for parent.</p>
  
  <p>and here is FolderDAO:</p>
  
  <pre><code>@Dao
  public interface FolderDAO {
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      public void insertFolder(Folder... folders);
  
      @Update
      public void updateFolder(Folder... folders);
  
  
      @Delete
      public void deleteFolders(Folder... folders);
  
      @Query("SELECT * FROM folders")
      List&lt;Folder&gt; getAll();
  
      @Query("SELECT * FROM folders WHERE id IN (:folderIds)")
      List&lt;Folder&gt; loadAllByIds(int[] folderIds);
  }
  </code></pre>
  
  <p>But when I make a folder object and try to insert it:</p>
  
  <pre><code>AsyncTask.execute(new Runnable() {
          @Override
          public void run() {
              Folder folder = new Folder();
              folder.setName("All");
  
              DatabaseInstance.getInstance(getApplicationContext()).folderDAO().insertFolder(folder);
  
          }
      });
  </code></pre>
  
  <p>get this error:</p>
  
  <p>FOREIGN KEY constraint failed</p>
  
  <pre><code>         --------- beginning of crash
  E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                Process: sahraei.hamidreza.com.note, PID: 1835
                android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
                    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
                    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                    at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:80)
                    at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:80)
                    at sahraei.hamidreza.com.note.DAO.FolderDAO_Impl.insertFolder(FolderDAO_Impl.java:80)
                    at sahraei.hamidreza.com.note.ItemListActivity$1.run(ItemListActivity.java:62)
                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                    at java.lang.Thread.run(Thread.java:818)
  </code></pre>
  
  <p>Does anyone know what's wrong or suggest another design for my project table?</p>