Android Room FOREIGN KEY约束失败

我试图在Android SQLite中设计和实现文件夹树结构,借助Google在I / O 2017中引入的Android Room Persistence(一种ORM)。在我的设计中,文件夹可以包含其他文件夹和文件。 这是我的文件夹和文件的代码:


@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 } 


 @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; } 



 @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 getAll(); @Query("SELECT * FROM folders WHERE id IN (:folderIds)") List loadAllByIds(int[] folderIds); } 


 AsyncTask.execute(new Runnable() { @Override public void run() { Folder folder = new Folder(); folder.setName("All"); DatabaseInstance.getInstance(getApplicationContext()).folderDAO().insertFolder(folder); } }); 



  --------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process:, 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( at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId( at android.database.sqlite.SQLiteStatement.executeInsert( at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert( at at at$ at android.os.AsyncTask$SerialExecutor$ at java.util.concurrent.ThreadPoolExecutor.runWorker( at java.util.concurrent.ThreadPoolExecutor$ at 


我得到了这个工作,但没有使用int主键。 我不是那种ORM场景的粉丝,只是因为这些问题。


 /*** Copyright (c) 2017 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package; import; import; import; import; import; import java.util.UUID; import static; @Entity( tableName="categories", foreignKeys=@ForeignKey( entity=Category.class, parentColumns="id", childColumns="parentId", onDelete=CASCADE), indices=@Index(value="parentId")) public class Category { @PrimaryKey public final String id; public final String title; public final String parentId; @Ignore public Category(String title) { this(title, null); } @Ignore public Category(String title, String parentId) { this(UUID.randomUUID().toString(), title, parentId); } public Category(String id, String title, String parentId) {; this.title=title; this.parentId=parentId; } } 


 @Query("SELECT * FROM categories WHERE parentId IS NULL") Category findRootCategory(); @Query("SELECT * FROM categories WHERE parentId=:parentId") List findChildCategories(String parentId);