question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Uncaught (in promise): Error: FOREIGN KEY constraint failed

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [x] sqljs [ ] websql

TypeORM version:

[ ] latest [ ] @next [x] 0.1.11 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Inside an ionic application I create the connection and populate it with data. My models contains relations from Product going down to other models, this mean in Product model I set up relation with cascade insert/updates so I only need to hydrate products and save instead saving all models separately.

Connection setup:

if (isBrowser(this.platform)) {
    dbOptions = {
        type: 'sqljs',
        autoSave: true,
        location: 'products',
    } as SqljsConnectionOptions;
}

return createConnection(Object.assign(dbOptions, {
    entities: this.getModelsToLoad(),
    synchronize: true,
    logging: true,
}));

After I populate database everything works fine, and then after I reload page, i can see queries ran in console, because synchronize: true, i assume. Last query is:

executing query:  CREATE TABLE "temporary_product" ("identifier" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "id" varchar NOT NULL, ....)
executing query:  INSERT INTO "temporary_product"("identifier", "id", ....) SELECT "identifier", "id", ... FROM "product"
executing query:  DROP TABLE "product"
executing query:  ROLLBACK

than I receive the error.

Could you guys help me on how to overcome this. My expected outcome would be that I can use same data after a page reload, since that’s happens a lot when developing in browser an ionic app. Foreign keys enabled are not allowing me to re-initialize database again from existent data.

Thanks in advance, David

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

10reactions
vitalii-t12commented, Sep 15, 2020

hey, issue still exists in sqlite + typeorm + typescript image any solutions?

0reactions
samisamsamcommented, Jul 12, 2018

I’m using cordova. I solved my problem by using query method, thank you

Le sam. 7 juil. 2018 à 16:15, Daniel Lang notifications@github.com a écrit :

@samisamsam https://github.com/samisamsam What database are you using? Are the queries similar to the original issue? If not please open a new issue and tag me.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/typeorm/typeorm/issues/1530#issuecomment-403240406, or mute the thread https://github.com/notifications/unsubscribe-auth/AO6v83KPrt20vz0eFkQTyqqLvBDHrNnAks5uERbpgaJpZM4RyIIx .

Read more comments on GitHub >

github_iconTop Results From Across the Web

sqlite3 "foreign key constraint failed"
The "problem" is that you have set a foreign key on table B. foreign key(id2) references A(id). This means that column id2 in...
Read more >
"FOREIGN KEY constraint failed" when user deletes its ...
Hi. I need my users to be able to delete, not just set active = False, their account. To accomplish this, I've tried...
Read more >
MySQL 5.7 Reference Manual :: 13.1.18.5 FOREIGN KEY ...
A foreign key constraint is defined on the child table. ... subclause is only defined for one FOREIGN KEY clause, cascading operations fail...
Read more >
A conflict with the foreign key constraint occurs when you ...
Fixes a problem in SQL Server 2005 in which a foreign key constraint conflict occurs when you update the case of the column...
Read more >
How to fix MySQL ERROR 1452 a foreign key constraint fails
The cause of this error is the values you're trying to put into the table are not available in the referencing (parent) table....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found