only-cliches / Nano-SQL

Universal database layer for the client, server & mobile devices. It's like Lego for databases.
https://nanosql.io
MIT License
783 stars 51 forks source link

Issue with Foreign keys on cordova database #158

Open only-cliches opened 5 years ago

only-cliches commented 5 years ago

From gitter channel, user @bakonyidani

Hello! Great library! However, I ran into a problem. I’m trying to use it for a Cordova app and have set up two tables with the second one having foreign keys to both the first one and itself. Everything seems fine, but I get an error if I try to delete any record from any of the two tables if it has or even had a record that referenced it (via a foreign key). Other records (where the foreign keys are undefined) can be deleted just fine. The relevant bits:

nSQL().createDatabase({
    ...
    tables: [
        {
            name: "project", model: {
                "id:uuid": { pk: true },
                "projectName:string": {},
            },
        },
        {
            name: "classDef",
            model: {
                "id:uuid": { pk: true },
                "className:string": {},
                "superclassID:uuid": {},
                "projectID:uuid": {},
            },
            indexes: {
                "superclassID:uuid": { foreignKey: { target: "classDef.id", onDelete: InanoSQLFKActions.CASCADE } },
                "projectID:uuid": { foreignKey: { target: "project.id", onDelete: InanoSQLFKActions.CASCADE } }
            }
        }
    ]
})
…
nSQL("classDef").query("delete").where(rec=>rec.id==id).exec().then(…).catch(…)});

The error I received (it is not caught by the .catch() at the end of the nSQL query):

Uncaught (in promise) DOMException: Failed to execute 'get' on 'IDBObjectStore': No key or key range specified.
at http://localhost:8000/index.bundle.js:480:35
at IndexedDB../node_modules/@nano-sql/core/lib/adapters/indexedDB.js.IndexedDB.store (http://localhost:8000/index.bundle.js:376:9)
at IndexedDB../node_modules/@nano-sql/core/lib/adapters/indexedDB.js.IndexedDB.read (http://localhost:8000/index.bundle.js:479:14)
at IndexedDB../node_modules/@nano-sql/core/lib/adapters/memoryIndex.js.nanoSQLMemoryIndex.readIndexKey (http://localhost:8000/index.bundle.js:706:14)
at http://localhost:8000/index.bundle.js:6516:25
at nanoSQL../node_modules/@nano-sql/core/lib/index.js.nanoSQL.doFilter (http://localhost:8000/index.bundle.js:1781:13)
at Object.readIndexKey (http://localhost:8000/index.bundle.js:6512:18)
at http://localhost:8000/index.bundle.js:5493:103
at http://localhost:8000/index.bundle.js:6750:13
at new Promise (<anonymous>)

The error only goes away if the foreign keys are set to ‘InanoSQLFKActions.NONE’, but this would leave me with orphaned / bad records. Any ideas? Thank you!

only-cliches commented 5 years ago

From gitter channel, user @bakonyidani P.s. I have just noticed that the delete works just fine on Android (cordova run android), only the browser mode of cordova seems to be effected (I do most of my debugging and developement in the browser mode).