Skip to content

FOREIGN Key problems #97

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

Open
langrock opened this issue May 25, 2025 · 2 comments
Open

FOREIGN Key problems #97

langrock opened this issue May 25, 2025 · 2 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@langrock
Copy link

Hi,

Thanks for putting together this tool for the general public. I have been trying it out on a few databases that contain tables with foreign-key constraints. This always failed unless suppressing the transfer of foreign keys using the -X option. Not the end of the world, but requires quite a bit of manual labor to restore the constraints afterwards. Could this possibly be due to the order with which the tool parses the various tables in the database? Maybe I simply don't understand how to properly use it or am missing a flag that would help avoid this problem.

Thanks

@techouse
Copy link
Owner

techouse commented May 26, 2025

Hey there,

To start, it would be great if you could provide some specific details.

The issue with SQLite, unlike MySQL, is that all foreign key constraints must be added to the tables at their creation time. In contrast, MySQL allows these constraints to be added after the tables have been created, so the order in which tables are created does matter.

Currently, the tables are just queried using the following MySQL query:

SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = SCHEMA()

and looped through. While the tables are created, SQLite foreign key checks are disabled

self._sqlite_cur.execute("PRAGMA foreign_keys=OFF")
and then finally re-enabled once completed
self._sqlite_cur.execute("PRAGMA foreign_keys=ON")

so most of your troubles should have been handled already, but without andy specifics around your case I can only guess.

Calculating a vector that would support arbitrary foreign key constraint creation would be quite complex, and I haven’t even attempted it.

TL;DR: SQLite doesn’t support ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY

@techouse techouse added enhancement New feature or request question Further information is requested labels May 26, 2025
@techouse techouse self-assigned this May 26, 2025
@langrock
Copy link
Author

Thanks so much for taking the time to explain the origins of this problem. I didn't realize that SQLite3 doesn't have that capability at the moment. I have been using SQLPro for SQLite on a Mac and the app allows for the creation of foreign keys after the fact, likely doing all the work behind the scenes to create a new table with foreign key constraints and then copying all the data over before dropping the original table. Learning something new every day ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants