question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FOREIGN KEY constraint failed (code 787)

See original GitHub issue

Hi, 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:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
agrosnercommented, Feb 16, 2016

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.

2reactions
agrosnercommented, Feb 16, 2016

Are you saving the ForeignKeyContainer associated Model 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 the ForeignKeyContainer object structure. you still need to save the parent object before saving the child RoomModel. What you can do is override the save() method and save the associated Model. You cannot save ForeignKeyContainer objects directly.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found