Database table not being created using SQLitePorter - Syntax Error


#1

Hi All,

So i’m having an issue with creating a specific table using the SQLitePorter plugin.

I have a file in my assets folder called initDB.sql, within this i have this SQL Line:

CREATE TABLE IF NOT EXISTS `localListItems` (
   `id` INTEGER NOT NULL ,
   `quantity` TINYINT(2) NOT NULL , 
   `checked` TINYINT(1) NOT NULL DEFAULT 0 ,
   `listId` INTEGER NOT NULL , 
   `itemId` INTEGER NOT NULL , 
    PRIMARY KEY (`id`), 
    KEY `fkIdx_26` (`listId`), 
    CONSTRAINT `FK_26` 
      FOREIGN KEY `fkIdx_26` (`listId`) 
      REFERENCES `localLists` (`id`) 
      ON DELETE CASCADE, 
    KEY `fkIdx_30` (`itemId`), 
    CONSTRAINT `FK_30` 
      FOREIGN KEY `fkIdx_30` (`itemId`) 
      REFERENCES `localItems` (`id`) 
      ON DELETE CASCADE
);

which is minified in the document and therefore takes up one line to prevent escaping errors ‘\n’

I am calling this file using the following typescript code:

this.httpClient.get('assets/initDB.sql', { responseType: 'text' })
      .subscribe((sql:any) => {
        this.sqlitePorter.importSqlToDb(this.database, sql)
          .then(data => {
            this.databaseReady.next(true);
            this.storage.set('database_filled', this.databaseVersionControl);
          })
          .catch(e => console.log(e));
      });

All the other tables are inserting as required (not shown in the above) however the SQL above works fine in PHPMyAdmin but is not working when trying to create it using SQLite.

The error appearing in the console is the following JSON.

{
  "code":5,
  "message":"Failed to import SQL; message=near \"KEY\": syntax error",
  "statement":"CREATE TABLE IF NOT EXISTS `localListItems` ( `id` INTEGER NOT NULL , `quantity` TINYINT(2) NOT NULL , `checked` TINYINT(1) NOT NULL DEFAULT 0 , `listId` INTEGER NOT NULL , `itemId` INTEGER NOT NULL , PRIMARY KEY (`id`), KEY `fkIdx_26` (`listId`), CONSTRAINT `FK_26` FOREIGN KEY `fkIdx_26` (`listId`) REFERENCES `localLists` (`id`) ON DELETE CASCADE, KEY `fkIdx_30` (`itemId`), CONSTRAINT `FK_30` FOREIGN KEY `fkIdx_30` (`itemId`) REFERENCES `localItems` (`id`) ON DELETE CASCADE)"
}

Any help as to why this isn’t working would be much appriciated.

Thank you.

  • Matt