SQLiteConstraintException: how to match "no relationship" with FOREIGN KEY in the room

I am using the Android Space Conservation Library as an ORM.

I have the following Entity:

@Entity(tableName = "log_entries",
        foreignKeys = {
                @ForeignKey(
                        entity = Serving.class,
                        parentColumns = "id",
                        childColumns = "foodId",
                        onDelete = ForeignKey.CASCADE
                )
        }
)
public class LogEntry {

    @PrimaryKey(autoGenerate = true)
    private long id;

    private long servingId;

    // ...
}

There are several log entries that have a service, and some do not. Adding an entry to the log that has the service works fine, but adding one with id = 0 to represent "no relationship" triggers SQLiteConstraintExceptiona message

FOREIGN KEY constraint failed

// ...
LogEntry logEntry = new LogEntry();
logEntry.setServingId(0);
// ...

db.logEntryDao().add(logEntry);

So, how can I express the fact that the journal entry does not work in the room?

0
source share
1 answer

Long null servingId.

// ...
LogEntry logEntry = new LogEntry();
logEntry.setServingId(null);
// ...

db.logEntryDao().add(logEntry);
+1

All Articles