2

I'm trying to create two tables dare_table and step_table and ran into the FOREIGN KEY constraint failed error (787) while inserting Steps.

I have followed these posts with no success:

I'm still new to Room, any help is appreciated!

Dare.class:

@Entity(tableName = "dare_table",
indices = {@Index(value = {"stepList"}, unique = true),

public class Dare {

@PrimaryKey(autoGenerate = true)
public int id;

public Dare(String dareTitle, String stepList) {
    this.dareTitle = dareTitle;
    this.stepList = stepList;
}

public void setId(int id) {
    this.id = id;
}

private String dareTitle;

@ColumnInfo(name = "stepList")
private String stepList;

public int getId() {
    return id;
}

public String getDareTitle() {
    return dareTitle;
}

public String getStepList() {
    return stepList;
 }
}

Step.class

@Entity(tableName = "step_table",
    indices = {@Index(value = {"stepName"})},
    foreignKeys = @ForeignKey(
            entity = Dare.class,
            parentColumns = "stepList",
            childColumns = "stepName"
    ))

public class Step {

@PrimaryKey //Gets the same error with and without autoGenerate = true
public int id;

@ColumnInfo(name = "stepName")
private String stepName;

private boolean isCompleted;

public Step(String stepName, boolean isCompleted) {
    this.stepName = stepName;
    this.isCompleted = isCompleted;
}

public void setId(int id) {
    this.id = id;
}

public int getId() {
    return id;
}

public String getStepName() {
    return stepName;
}

public boolean isCompleted() {
    return isCompleted;
 }
}

The doInBackground (in a Repository class) as referenced in Error Log

    private static class InsertStepAsyncTask extends AsyncTask<Step, Void, Void> {

    private StepDao stepDao;

    private InsertStepAsyncTask(StepDao stepDao) {
        this.stepDao = stepDao;
    }

    @Override
    protected Void doInBackground(Step... steps) {
        stepDao.insert(steps[0]);
        return null;
    }
}

Error Log:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.kyperstudios.daretowork, PID: 9845
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:365)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:784)
 Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (Sqlite code 787), (OS error - 0:Success)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:818)
    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.kyperstudios.daretowork.Data.StepDao_Impl.insert(StepDao_Impl.java:89)
    at com.kyperstudios.daretowork.Data.DareRepository$InsertStepAsyncTask.doInBackground(DareRepository.java:164)
    at com.kyperstudios.daretowork.Data.DareRepository$InsertStepAsyncTask.doInBackground(DareRepository.java:154)
    at android.os.AsyncTask$2.call(AsyncTask.java:345)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:257) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
    at java.lang.Thread.run(Thread.java:784) 
LittleNobody
  • 55
  • 1
  • 7

1 Answers1

0

you may try to insert the a step with a name which is a foreign key to non exists element in the Dare class. so you have first to insert Dare object with stepList field you want to reference in the step class then you can insert the step successfully

daredao().insertAll(new Dare("dare_title1", steps[0].getStepName()));
stepdao().insertAll(steps[0]);
Ramy Ibrahim
  • 656
  • 4
  • 19