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

PRAGMA foreign_keys not working #23

Open
Ace-GIS opened this issue Oct 12, 2017 · 2 comments
Open

PRAGMA foreign_keys not working #23

Ace-GIS opened this issue Oct 12, 2017 · 2 comments

Comments

@Ace-GIS
Copy link

Ace-GIS commented Oct 12, 2017

I am using the following snippet on an Android Samsung Galaxy Tab S2 without success.

initDatabase: function () {
        window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(externalDataDirectoryEntry) {
            //window.sqlitePlugin.deleteDatabase({name: 'event_manager.db', androidDatabaseLocation: externalDataDirectoryEntry.toURL()});
            db = window.sqlitePlugin.openDatabase({name: 'event_manager.db', androidDatabaseLocation: externalDataDirectoryEntry.toURL()});

            db.executeSql('PRAGMA foreign_keys = ON;', [], function(res) {
                db.executeSql('PRAGMA foreign_keys;', [], function(res){
                    console.log('PRAGMA res: ' + JSON.stringify(res));
                })
            })

            db.transaction(function(tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS events( EventID Integer Primary Key NOT NULL, RaceName Text NOT NULL, '
                    + 'BeginDate DateTime NOT NULL, EndDate DateTime NOT NULL, LocationName Text, '
                    + 'LocationAddrLine1 Text, LocationAddrLine2 Text, LocationPostCode Integer, '
                    + 'LocationCity Text, LocationCountry Text, CrossRefID Integer NOT NULL);');

                tx.executeSql('CREATE TABLE IF NOT EXISTS groups(EventID Integer Primary Key NOT NULL, GroupID Integer NOT NULL, ' //Primary Key
                    + 'GroupName Text, HasComments Boolean NOT NULL, FOREIGN KEY(EventID) REFERENCES events(EventID));');

                tx.executeSql('CREATE TABLE IF NOT EXISTS questions(GroupID Integer Primary Key NOT NULL, QuestionID Integer NOT NULL, ' //Primary Key
                    + 'AnswerType Text NOT NULL, SortOrder Integer NOT NULL, QuestionText Text NOT NULL, '
                    + 'FOREIGN KEY(GroupID) REFERENCES groups(GroupID));');

                tx.executeSql('CREATE TABLE IF NOT EXISTS comments(GroupID Integer Primary Key NOT NULL, CommentText Text, ' //Primary Key
                    + 'FOREIGN KEY(GroupID) REFERENCES groups(GroupID));');

                tx.executeSql('CREATE TABLE IF NOT EXISTS answers(GroupID Integer Primary Key NOT NULL, QuestionID Integer NOT NULL, '
                    + 'Yes Integer, No Integer, MCAnswer1 Integer, MCAnswer2 Integer, MCAnswer3 Integer, MCAnswer4 Integer, '
                    + 'MCAnswer5 Integer, DateTime DateTime, Quantity Integer, AnswerText Text, '
                    + 'FOREIGN KEY(GroupID) REFERENCES groups(GroupID), FOREIGN KEY(QuestionID) REFERENCES questions(QuestionID));');

                tx.executeSql('CREATE TABLE IF NOT EXISTS file_upload(EventID Integer NOT NULL, Purpose Text, Description Text, '
                    + 'RidernetID Text, QuestionID Integer, TempInjuryID Integer, MediaType Text, MediaContent Text, '
                    + 'MediaFileName Text, CrossRefID Integer, FOREIGN KEY(EventID) REFERENCES events(EventID), '
                    + 'FOREIGN KEY(QuestionID) REFERENCES questions(QuestionID), FOREIGN KEY(CrossRefID) REFERENCES events(CrossRefID));');

                tx.executeSql('CREATE TABLE IF NOT EXISTS media(EventID Integer NOT NULL, mediaItemPath Text, '
                    + 'FOREIGN KEY(EventID) REFERENCES events(EventID));');

            }, function(error) {
                console.log('Transaction ERROR: ' + error.message);
                db.close();
            }, function() {
                console.log('Tables created - Database OK');
            });
        });
    }

Not sure why the begining is not shown as code. Sorry...

When I open the database to check using "PRAGMA foreign_keys;" the result is 0 which is OFF. Can anybody help me enable the foreign keys?

@brodycj
Copy link
Contributor

brodycj commented Nov 24, 2017

My apologies for the delay. I hope to test this within the next few weeks. Labeling as a bug for now.

@brodycj
Copy link
Contributor

brodycj commented Jan 15, 2018

I fixed JavaScript syntax highlighting in the original question for better readability, added your question to the list of items for the next minor release of cordova-sqlite-storage.

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

No branches or pull requests

2 participants