On Wed May 10, 2017 at 08:34:42AM +0200, Clemens Ladisch wrote:
> Mark Wagner wrote:
> > Is there a way to get sqlite to tell which foreign key constraint is
> > causing a failure?
> 
> No; to make the implementation of deferred constraints easier, it keeps
> track only of the number of remaining foreign key failures, not of their
> origin.

Maybe; If you are developing and can afford to reload/reset invalid
data you can turn off foreign key support and do the DELETE, and then
run the foreign_key_check pragma:

    CREATE TABLE a(
        id INTEGER PRIMARY KEY
    );

    CREATE TABLE b(
        id INTEGER,
        FOREIGN KEY(id) REFERENCES a(id)
    );

    INSERT INTO a VALUES(1);
    INSERT INTO b VALUES(1);
    DELETE FROM a;
    -- Error: FOREIGN KEY constraint failed

    PRAGMA foreign_keys=0;
    DELETE FROM a;
    -- No Error, but now your data is invalid

    PRAGMA foreign_key_check;
    --  table       rowid       parent      fkid
    --  ----------  ----------  ----------  ----------
    --  b           1           a           0

You could perhaps even run the DELETE inside a transaction and rollback
once you have obtained the information you need, to keep your data
integrity.

Regards,
Mark
-- 
Mark Lawrence
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to