sqlite: DELETE FROM in iOS incorrectly UPDATES wrong column, resulting in "no such column" exception

Describe the bug When deleting an entry from Table_1 which has a FOREIGN KEY to Table_2, on iOS an exception occurs.

To Reproduce

CREATE TABLE IF NOT EXISTS Table_1 (
      id INTEGER PRIMARY KEY NOT NULL,
      result_id TEXT,
      result_slug TEXT,

      sql_deleted BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1)),
      last_modified INTEGER DEFAULT (strftime('%s', 'now')),

      FOREIGN KEY (result_id, result_slug) REFERENCES Table_2(id, slug) ON DELETE SET DEFAULT
    );
CREATE TABLE IF NOT EXISTS Table_2 (
      id TEXT NOT NULL,
      slug TEXT NOT NULL,

      sql_deleted BOOLEAN DEFAULT 0 CHECK (sql_deleted IN (0, 1)),
      last_modified INTEGER DEFAULT (strftime('%s', 'now')),

      PRIMARY KEY (id, slug)
    );

I’ve debugged through XCode what’s happening and

  • On executing DELETE FROM Table_1 WHERE id = ?
  • findReferencesAndUpdate gets called, and the reference columns are incorrectly calculated
  • (line 576, UtilsSQLCipher.swift) - stmt gets calculated incorrectly as UPDATE Table_2 SET sql_deleted = 1 WHERE result_id = ? instead of UPDATE Table_2 SET sql_deleted = 1 WHERE id = ?.
  • Basically, it uses the column of Table_1 to update reference of Table_2.

Screenshots image

Smartphone (please complete the following information):

  • Device: iPhone 13
  • OS: iOS 16

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 29

Most upvoted comments

That sounds perfect to me - I’ll gladly test the new version when you release it, thanks!

@MarcoG3 thank for providing the code it helps to identify the issue. I have now to solve it which will take me some times .