FOREIGN KEY constraint failed (code 787)
See original GitHub issueHi, I have the following schema on DB Flow.
`
@ModelContainer
@Table(database = BookingDatabase.class)
public class ComposedBooking extends BaseModel
public List<RoomModel> roomModels = new ArrayList<>();
@OneToMany(methods = {OneToMany.Method.ALL}, variableName = "roomModels")
public List<RoomModel> getRoomModels() {
if (roomModels == null) {
roomModels = new SQLite().select()
.from(RoomModel.class)
.where((RoomModel_Table.room_booking_id).eq(id))
.queryList();
}
return roomModels;
}
@ModelContainer
@Table(database = BookingDatabase.class)
public class RoomModel extends BaseModel
@Column
@ForeignKey(references = {@ForeignKeyReference(columnName = "room_booking_id", columnType = Long.class, foreignKeyColumnName = "id")}, saveForeignKeyModel = false, onDelete = ForeignKeyAction.CASCADE, onUpdate = ForeignKeyAction.CASCADE)
public ForeignKeyContainer<ComposedBooking> composedBookingRef;
public void setRoomModelComposedBookingKey(ComposedBooking composedBooking) {
if(composedBooking == null){
Timber.e("Composed booking is null");
return;
}
Timber.v("Composed booking not set");
composedBookingRef = FlowManager.getContainerAdapter(ComposedBooking.class).toForeignKeyContainer(composedBooking);
}
`
However, when I associate the table RoomModel
with the ComposedBooking
class.
When I call
roomModel.setRoomModelComposedBookingKey(composedBooking); roomModel.save();
.
It throws the error
FOREIGN KEY constraint failed (code 787) android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
However if call save()
before associating the foreign key , it works. Why am I not able to save after associating the foreign key?
I am using
'def dbflow_version = "3.0.0-beta2"'
as default.
Please help. Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
SQLite Foreign Key Constraint Failed (code 787)
The error here is related to creating a child entity before their parent is in existence. The Flow should be: Create Parent and...
Read more >android.database.sqlite.SQLiteConstraintException FOREIGN ...
SQLiteConstraintException FOREIGN KEY constraint failed (code 787). 2. Hotlists ... to a foreign key constraint violation because the original WorkSpec
Read more >FOREIGN KEY constraint failed (code 787) · Issue #368 - GitHub
I have tried a combination of saving order saveForeignKeyModel = false and @unique(onUniqueConflict = ConflictAction.REPLACE) for primary keys.
Read more >crash in android.database.sqlite.SQLiteConstraintException
SQLiteConstraintException : FOREIGN KEY constraint failed (code 787) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native ...
Read more >Android Room FOREIGN KEY constraint failed (code 787)
I'm tying to create a database in Android Room with two foreign keys. Every time I try to insert a track into the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hope that makes sense. SQLite works that way - you can save a NULL or an existing Foreign Key on an object with that reference. But it must exist first. and you cannot delete the parent until children are taken care of.
Are you saving the
ForeignKeyContainer
associatedModel
object before associating it and saving it to the DB? So what was fixed in beta-3 is that we now save the associated foreign key object before saving the child to the DB, but thats only if you forgo theForeignKeyContainer
object structure. you still need to save the parent object before saving the childRoomModel
. What you can do is override thesave()
method and save the associatedModel
. You cannot saveForeignKeyContainer
objects directly.