Problem: With Android Room, I'm receiving an foreign key constraint error on a join table when entering zero (0). I'm still trying to learn Android Room so I'm sure I have overlooked something in the documentation and tutorials.
What I have tried: I do have this application in a desktop version using SQLite and I have no issue and have tried to set it up the same. I've read some posts here and elsewhere with examples, and even removed the foreign key declarations that would return zero, but then I had difficulty with the schema starting to trigger on a different table altogether. I tried default values but and setting up for null but can't seem to get it to work. I will still be re-reading some at the time of this post but I'm sure I'm just overlooking some thing - the old 80/20 rule.
What I'm trying to do: This join table captures ids from 6 tables. However, based on the user entries, there are 3 foreign keys which it is possible the user did not add anything to be referenced. Since Android Room is in essence wrapping the SQLite for ease of use, so far, I haven't been able to determine how to get it to accept zero values on foreign keys. I thought about try an OnConflict
annotation but wanted to reach out for some thoughts and suggestions.
ENTITY
@Entity(tableName = "Notes", foreignKeys = {
@ForeignKey(entity = Sources.class, parentColumns = "SourceID", childColumns = "SourceID"),
@ForeignKey(entity = Comments.class, parentColumns = "CommentID", childColumns = "CommentID"),
@ForeignKey(entity = Questions.class, parentColumns = "QuestionID", childColumns = "QuestionID"),
@ForeignKey(entity = Quotes.class, parentColumns = "QuoteID", childColumns = "QuoteID"),
@ForeignKey(entity = Terms.class, parentColumns = "TermID", childColumns = "TermID"),
@ForeignKey(entity = Topics.class, parentColumns = "TopicID", childColumns = "TopicID")},
indices = {@Index("SourceID"), @Index("CommentID"), @Index("QuestionID"), @Index("QuoteID"),
@Index("TermID"), @Index("TopicID")})
public class Notes {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "NoteID")
private int noteID;
@ColumnInfo(name = "SourceID")
private int sourceID;
@ColumnInfo(name = "CommentID")
private int commentID;
@ColumnInfo(name = "QuestionID", defaultValue = "0")
private int questionID;
@ColumnInfo(name = "QuoteID", defaultValue = "0")
private int quoteID;
@ColumnInfo(name = "TermID", defaultValue = "0")
private int termID;
@ColumnInfo(name = "TopicID")
private int topicID;
@ColumnInfo(name = "Deleted", defaultValue = "0")
private int deleted;
public Notes(int noteID, int sourceID, int commentID, int questionID, int quoteID, int termID, int topicID, int deleted){
this.noteID = noteID;
this.sourceID = sourceID;
this.commentID = commentID;
this.questionID = questionID;
this.quoteID = quoteID;
this.termID = termID;
this.topicID = topicID;
this.deleted = deleted;
}
SCHEMA PORTION
"tableName": "Notes",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`NoteID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `SourceID` INTEGER NOT NULL, `CommentID` INTEGER NOT NULL, `QuestionID` INTEGER NOT NULL DEFAULT 0, `QuoteID` INTEGER NOT NULL DEFAULT 0, `TermID` INTEGER NOT NULL DEFAULT 0, `TopicID` INTEGER NOT NULL, `Deleted` INTEGER NOT NULL DEFAULT 0, FOREIGN KEY(`SourceID`) REFERENCES `Sources`(`SourceID`) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(`CommentID`) REFERENCES `Comments`(`CommentID`) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(`QuestionID`) REFERENCES `Questions`(`QuestionID`) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(`QuoteID`) REFERENCES `Quotes`(`QuoteID`) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(`TermID`) REFERENCES `Terms`(`TermID`) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(`TopicID`) REFERENCES `Topics`(`TopicID`) ON UPDATE NO ACTION ON DELETE NO ACTION )",
"fields": [
{
"fieldPath": "noteID",
"columnName": "NoteID",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "sourceID",
"columnName": "SourceID",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "commentID",
"columnName": "CommentID",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "questionID",
"columnName": "QuestionID",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "quoteID",
"columnName": "QuoteID",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "termID",
"columnName": "TermID",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "topicID",
"columnName": "TopicID",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "deleted",
"columnName": "Deleted",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
}
],
"primaryKey": {
"columnNames": [
"NoteID"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_Notes_SourceID",
"unique": false,
"columnNames": [
"SourceID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_SourceID` ON `${TABLE_NAME}` (`SourceID`)"
},
{
"name": "index_Notes_CommentID",
"unique": false,
"columnNames": [
"CommentID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_CommentID` ON `${TABLE_NAME}` (`CommentID`)"
},
{
"name": "index_Notes_QuestionID",
"unique": false,
"columnNames": [
"QuestionID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_QuestionID` ON `${TABLE_NAME}` (`QuestionID`)"
},
{
"name": "index_Notes_QuoteID",
"unique": false,
"columnNames": [
"QuoteID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_QuoteID` ON `${TABLE_NAME}` (`QuoteID`)"
},
{
"name": "index_Notes_TermID",
"unique": false,
"columnNames": [
"TermID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_TermID` ON `${TABLE_NAME}` (`TermID`)"
},
{
"name": "index_Notes_TopicID",
"unique": false,
"columnNames": [
"TopicID"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Notes_TopicID` ON `${TABLE_NAME}` (`TopicID`)"
}
],
"foreignKeys": [
{
"table": "Sources",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"SourceID"
],
"referencedColumns": [
"SourceID"
]
},
{
"table": "Comments",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"CommentID"
],
"referencedColumns": [
"CommentID"
]
},
{
"table": "Questions",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"QuestionID"
],
"referencedColumns": [
"QuestionID"
]
},
{
"table": "Quotes",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"QuoteID"
],
"referencedColumns": [
"QuoteID"
]
},
{
"table": "Terms",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"TermID"
],
"referencedColumns": [
"TermID"
]
},
{
"table": "Topics",
"onDelete": "NO ACTION",
"onUpdate": "NO ACTION",
"columns": [
"TopicID"
],
"referencedColumns": [
"TopicID"
]
}
]
},