Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade delete #1610

Closed
jaczura1977 opened this issue Dec 31, 2021 · 3 comments
Closed

Cascade delete #1610

jaczura1977 opened this issue Dec 31, 2021 · 3 comments

Comments

@jaczura1977
Copy link

Hi!
I don't know if it is an SQLite or drift issue, but deleting parent entities leave the child entities undeleted with invalid data of foreign key (Id that not exists in parent entity as Id.)

CREATE TABLE IF NOT EXISTS todo (id TEXT NOT NULL, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS todoElement (id TEXT NOT NULL, todoId TEXT NOT NULL REFERENCES todo (id), PRIMARY KEY (id));

@kuhnroyal
Copy link
Contributor

You probably need to enable foreign keys.

@simolus3
Copy link
Owner

simolus3 commented Jan 1, 2022

Yes, foreign keys aren't enabled by default. My suggestion is to add a beforeOpen callback to your migration and enable foreign keys there:

  MigrationStrategy get migration => MigrationStrategy(
    beforeOpen: (details) async {
      await customStatement('PRAGMA foreign_keys = ON');
    },
  );

@simolus3 simolus3 closed this as completed Jan 1, 2022
@Mike278
Copy link
Contributor

Mike278 commented Jan 1, 2022

Enabling foreign keys will prevent the database from getting into an inconsistent state, but your schema is configured to throw an error when attempting to delete the parent. If you instead want to cascade delete children on parent deletion, you need to configure that explicitly - see https://www.sqlite.org/foreignkeys.html#fk_actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants