-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Unable to perform migrations for sqlite. Foreign key constraint error #2800
Comments
I need better reproduction for this, what you described is already in tests. Please provide at least the entity definition, and be sure to upgrade to latest before reporting things.
That's mostly knex doing this magic, unfortunately schema alters in sqlite are quite problematic due to lots of limitations with FKs, so using this temp table is usually the only way to do more complex alters. |
I did some more digging and found the answer and thought I'd document it for all that follow. Sorry for the wall of text lol. Turns out it's a little of not understanding the migrations fully, the migrations getting it what I would call a bad state and not reading the documentation fully. When I created the first migration I used the I then proceeded to put data into the database and use it as normal with no issues. Time came to add another column to the data and I did this and then ran the At this point it makes sense that it's failing as the "many" table won't let the "one" table be dropped because of the foreign key relationship. This is why I say it allows you to get into a bad state as you will never be able to get any successful migrations as far as I understand what's going on. For a sanity check I removed all the migrations dropped the DB to start the migrations from scratch. I ran
As for my not reading the docs perfectly I overlooked how important the snapshot was for the migrations as the
However I've verified this is not the case for Anyway thanks for getting back to me so quick and I have to say although the documentation for using mikro-orm with nestjs (both frameworks docs for integrating are equally missing the finer details), mikro-orm has been pretty good to use and will keep it in mind for future projects. I won't close this as I think it's important that someone reads it first to understand how I got it into this state but feel free to close it off as I've fixed the issue now. :) |
The
It is indeed a bug that it won't generate the snapshot. But this should still not really happen, I would still like to see your entity definition as there should be no alter queries even without the snapshot (I say alter but for sqlite it ends up with temp tables and recreating). |
Re-reading that section I feel like it could be better written to make that the --initial is optional and that if you have generated a schema (via I've included my entity definitions below. The second migration is just uncommenting the property
|
Well, I didn't really like the idea of even having this initial thing. Migrations do schema diffing, if there is no schema, it will create it. So for the regular flow, there is no need for it, just create the migration when you want to sync your entities with (empty or not) schema. Are you using ts-morph? If not, your entity definition is missing some things (or maybe just one thing - |
Yes I'm using ts-morph (my current config is below). I'm using nestjs so have been following the docs from here (apart from the normal mikro-orm docs). They all don't have the whole picture and have had to stitch/reverse engineer them to get it all linked up and working. |
I am happy to accept PRs or specific suggestions on how to improve it, but don't expect to find every information in a recipe about nest integration, as that should really be just about the integration and not about ORM features. Btw why do you disable the context middleware? If its because of ALS, that is enabled by default in v5, so no need to separate context handling. Note that the version on nest docs is a bit outdated, will PR that once we have v5 of the nest package out, as there will be more changes in the docs. It is basically a copy of what we have in the recipe in our docs (which will be always more up to date). |
I agree that not all the information should be there. However both nest and orm did not support typescript Config files out of the box. I had to carefully merge my code into the sample to find things like a index.js file, adding main to package .json and such as I would only get an error about not having the .js Config. No idea on the context. I just copied and modified the Config from working sample project. I’ll give it a go without that |
Btw the problem is with Unfortunately I am not sure if we can infer this from information schema, maybe by parsing the table definition swich quite sucks as it can be fragile, but what else can we do :] |
Describe the bug
Cannot apply migrations to sqlite database
Stack trace
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Migration should succeed, data should still be in table and new table should be set.
Additional context
This is the migration that is generated
I exported the sql and ran it directly on the sqlite database and it worked successfully. It's almost like it's not setting the ignore foreign key or something??
What's also a little strange is I'm not sure why the migration which only adds a column to
sequence
table (the many) drops and recreates thedoor
table (the one)?I've played around with the
dropTables
,disableForeignKeys
,transactional
, andallOrNothing
to see if it makes a difference and it doesn't. I have also tried running the "up" via cli and in code and the result is the same.Versions
The text was updated successfully, but these errors were encountered: