Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To rebuild a SQLite table without foreign-key errors, turn foreign-key enforcement off on the migration connection before starting a transaction, rebuild the table and its dependent schema objects, run PRAGMA foreign_key_check, commit, and then restore the connection’s original enforcement setting. Changing PRAGMA foreign_keys after BEGIN has no effect.

Use SQLite’s safe table-rebuild order

SQLite documents this procedure for schema changes that cannot be handled by its supported direct ALTER TABLE operations. The table definition and data mapping below are examples only: adapt them to your schema. Before starting, save the SQL for the table’s indexes and triggers and identify views that depend on it.

  1. Check and disable enforcement on the migration connection, before a transaction. Run PRAGMA foreign_keys;, then PRAGMA foreign_keys = OFF;, then query PRAGMA foreign_keys; again to confirm the setting. The setting is per connection. It cannot be changed while a transaction or savepoint is pending.
  2. Start the transaction. Run BEGIN; only after confirming the connection’s setting.
  3. Create the replacement table and copy the data. Use the intended columns and constraints, and specify matching column lists so the copy maps each value to the right destination.
  4. Drop the original and rename the replacement. Use DROP TABLE on the old table, then rename the new table to the original name.
  5. Restore dependent schema objects and validate. Recreate saved indexes and triggers, and drop and recreate views if their definitions are affected. Run PRAGMA foreign_key_check; before committing.
  6. Commit and restore enforcement. If validation returns no rows and the migration is otherwise sound, run COMMIT;, then restore the setting that was in effect before the migration. Query PRAGMA foreign_keys; to confirm.

SQLite’s ALTER TABLE guidance describes the rebuild sequence, while its PRAGMA reference documents the enforcement and validation commands.

SQL outline

-- On the migration connection, before BEGIN:
PRAGMA foreign_keys;
PRAGMA foreign_keys = OFF;
PRAGMA foreign_keys;

BEGIN;

-- Save dependent indexes, triggers, and affected views before rebuilding.
CREATE TABLE new_X (
  -- desired columns and constraints
);

INSERT INTO new_X (column_a, column_b)
SELECT column_a, column_b
FROM X;

DROP TABLE X;
ALTER TABLE new_X RENAME TO X;

-- Recreate saved indexes and triggers; update affected views.
PRAGMA foreign_key_check;
-- Investigate and repair any returned rows before accepting the migration.

COMMIT;

-- Restore the original enforcement setting after commit.
PRAGMA foreign_keys = ON;
PRAGMA foreign_keys;

The final command shown restores enforcement only if it was originally on; preserve the actual prior state rather than blindly copying that line. If the check reports violations, do not treat the rebuild as successful or commit unresolved problems. Inspect the reported child table and row, referenced parent, and constraint index; check the definitions and data mapping, then repair the issue or roll back as appropriate.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Diagnose the error you are seeing

PRAGMA foreign_keys = OFF seems ignored

Check whether the connection already has a transaction or savepoint open. SQLite treats the setting change as a no-op in that state. Issue it before BEGIN, on the same connection that runs the migration, then query the pragma to confirm its state. Foreign-key enforcement is set per connection; do not assume another connection’s setting applies. See SQLite Foreign Key Support and the PRAGMA reference.

DROP TABLE fails

With foreign keys enabled, dropping a table performs an implicit delete of its rows. That delete can invoke foreign-key actions or violate constraints. An immediate violation can make the drop fail; a deferred violation can surface at commit if it remains unresolved. The documented rebuild procedure disables enforcement before the transaction and validates the reconstructed database afterward. See SQLite Foreign Key Support.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

foreign key mismatch or no such table

These errors can point to an invalid relationship declaration rather than a failed data copy. Confirm that the referenced parent table and columns exist, and that the referenced parent columns form a primary key or a suitable unique key. Run PRAGMA foreign_key_list(child_table); to inspect the child’s declaration, then compare it with the parent table definition and indexes. SQLite’s foreign-key guide explains these configuration errors; the PRAGMA reference documents foreign_key_list.

PRAGMA foreign_key_check returns rows

Each returned row describes a foreign-key violation: the child table, offending rowid (or NULL for a WITHOUT ROWID child), referenced parent table, and foreign-key constraint index. Use those details to locate the affected relationship and inspect the child data and key definitions. The check belongs before commit; unresolved rows mean the migration has not been verified. See the PRAGMA reference and ALTER TABLE guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Do not use deferred constraints as a replacement

PRAGMA defer_foreign_keys=ON postpones all foreign-key checks until the outermost transaction commits, regardless of how the constraints were declared. SQLite resets this setting at each commit or rollback, so it must be enabled again for each transaction. Deferral changes when violations are checked; it does not repair invalid references or replace the rebuild-and-check procedure. See the PRAGMA reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check rename behavior on older SQLite versions

SQLite 3.26.0, released December 1, 2018, changed how renaming a parent table updates references: from that version onward, references are updated even when PRAGMA foreign_keys is off, unless PRAGMA legacy_alter_table=ON. Before 3.26.0, reference updates depended on foreign-key enforcement being on. If a rename behaves unexpectedly, check the runtime SQLite version and the legacy setting. See SQLite’s ALTER TABLE documentation.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.