Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:37:46+00:00 2026-06-03T07:37:46+00:00

Just stumbled upon the fact foreign keys are disabled by default in sqlite. I

  • 0

Just stumbled upon the fact foreign keys are disabled by default in sqlite. I have “on delete casdade” foreign key and deleting parent table records does not delete child records. Various posts indicate you need to enable that on each connection “PRAGMA foreign_keys = ON; “. So, where to enable it in FMDatabase? I would prefer setting some setting instead of running the command before each SQL stmt. PS. I’m using FMDatabaseQueue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:37:47+00:00Added an answer on June 3, 2026 at 7:37 am

    I quickly checked and PRAGMA foreign_keys = ON; works fine for me both on 5.1 simulator and 5.1 iPod Touch. As ccgus suggests, you should cache the data base connection. If you use the queue, just cache the queue and reorganize your code so it doesn’t create new queue each time you need to use the database. With your current approach, what is the point of having a queue if you don’t really use it but create new each time?

    But back to the question, as you are already aware, foreign keys are off by default, so you need to first enable it. I managed to do it with PRAGMA foreign_keys = ON;, here is some more of the test code I used:

    //create database
    NSString* dbPath = [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    dbPath = [dbPath stringByAppendingPathComponent:@"test.db"];
    db = [FMDatabase databaseWithPath:dbPath];
    if ([db open]) {
        NSLog(@"Database %@ opened", dbPath);
        //check for foreign_key
        NSString* sql = @"PRAGMA foreign_keys";
        FMResultSet *rs = [db executeQuery:sql];
        int enabled;
        if ([rs next]) {
            enabled = [rs intForColumnIndex:0];
        }
        [rs close];
        if (!enabled) {
            // enable foreign_key
            sql = @"PRAGMA foreign_keys = ON;";
            [db executeUpdate:sql];
            // check if successful
            sql = @"PRAGMA foreign_keys";
            FMResultSet *rs = [db executeQuery:sql];
            if ([rs next]) {
                enabled = [rs intForColumnIndex:0];
            }
            [rs close];
        }
        // do your stuff here, or just cache the connection
    } else {
        NSLog(@"Failed to open %@", dbPath);
    }
    

    Looks fairly straightforward, the only thing that comes to mind is that you used executeQuery instead of executeUpdate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just stumbled upon propertyGrid and its awesome! However, i have one task that i
I have just stumbled upon the bash syntax: foo=42 bar=$[foo+1] # evaluates an arithmetic
I've just began to use the GAE and I have stumbled upon an issue
I just stumbled upon a change that seems to have counterintuitive performance ramifications. Can
I just recently stumbled upon the fact that Declarative Services in OSGi can set
I have just stumbled upon Bad Behavior - a plugin for PHP that promises
I just stumbled upon a behavior which surprised me: When writing: int x =
I've just stumbled upon Fabric and the documentation doesn't really make it obvious how
I just read this question and stumbled upon the following quote: Scala treats ==
I've just started with db4o and I stumbled on to a problem. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.