在数据库上运行@test时出现以下错误:
android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (Sqlite code 787 SQLITE_CONSTRAINT_FOREIGNKEY), (OS error - 11:Try again)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:841)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:803)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51)
at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
at com.example.qwez.repository.local.QuestionDao_Impl$5.call(QuestionDao_Impl.java:138)
at com.example.qwez.repository.local.QuestionDao_Impl$5.call(QuestionDao_Impl.java:133)
at io.reactivex.internal.operators.completable.CompletableFromCallable.subscribeActual(CompletableFromCallable.java:36)
at io.reactivex.Completable.subscribe(Completable.java:2302)
at io.reactivex.Completable.blockingAwait(Completable.java:1219)
at com.example.qwez.repository.local.GameQuestionDaoTest.storeAndGet(GameQuestionDaoTest.java:63)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2252)
我的测试:
@RunWith(AndroidJUnit4.class)
public class GameQuestionDaoTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
private GameDatabase gameDatabase;
private GameDao gameDao;
private QuestionDao questionDao;
@Before
public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
gameDatabase = Room.inMemoryDatabaseBuilder(context, GameDatabase.class)
.allowMainThreadQueries()
.build();
gameDao = gameDatabase.gameDao();
questionDao = gameDatabase.questionDao();
}
@After
public void closeDb() throws IOException {
gameDatabase.close();
}
@Test
public void storeAndGet() {
Game game = new Game("cat","diff");
int id = (int) gameDao.insertReturnId(game);
for(int i=0;i<5;i++){
Question question = new Question("1", "2", "3", "4", "5");
question.setId(id);
questionDao.insert(question).blockingAwait();
}
int a = questionDao.getAll().blockingGet().size();
assertEquals(4, a);
}
}
我的实体:
@Entity(tableName = "games")
public class Game {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int gameId;
}
和
@Entity(foreignKeys = @ForeignKey(entity = Game.class,
parentColumns = "id",childColumns = "question_id",
onDelete = ForeignKey.CASCADE),
tableName = "questions")
public class Question {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
}
在Stackoverflow上找到了类似的问题,但没有找到任何可以帮助我解决这个问题的方法。有什么建议吗?
答案 0 :(得分:3)
您似乎将问题的 id 列设置为游戏ID,而不是 question_id 列,该列应包含游戏ID为每个childColumns = "question_id",
。
因此 question_id 列为空,因此发生冲突。
我相信您想要
question.setQuestion_Id(id);
代替
question.setId(id);