[#55866] Fix SQLite3 data loss during table alterations with CASCADE foreign keys.#55907
Merged
byroot merged 1 commit intorails:mainfrom Oct 17, 2025
Merged
[#55866] Fix SQLite3 data loss during table alterations with CASCADE foreign keys.#55907byroot merged 1 commit intorails:mainfrom
byroot merged 1 commit intorails:mainfrom
Conversation
7f90343 to
fafa7fe
Compare
032fe51 to
20d69d5
Compare
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
Outdated
Show resolved
Hide resolved
skipkayhil
reviewed
Oct 15, 2025
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
Outdated
Show resolved
Hide resolved
20d69d5 to
9764553
Compare
…CADE foreign keys.
9764553 to
ffd25a5
Compare
byroot
added a commit
that referenced
this pull request
Oct 17, 2025
[#55866] Fix SQLite3 data loss during table alterations with CASCADE foreign keys.
byroot
added a commit
that referenced
this pull request
Oct 17, 2025
[#55866] Fix SQLite3 data loss during table alterations with CASCADE foreign keys.
byroot
added a commit
that referenced
this pull request
Oct 17, 2025
[#55866] Fix SQLite3 data loss during table alterations with CASCADE foreign keys.
Member
|
Thank you. Backported to 8.1, 8.0 and 7.2 |
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
Open
4 tasks
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
emirn
added a commit
to emirn/rails
that referenced
this pull request
Apr 2, 2026
… foreign keys inside `ActiveRecord::Base.connection.transaction`. Silent cascade removal of child records was fixed in rails#55907 but when table alteration code was wrapped by `ActiveRecord::Base.connection.transaction`, SQLite3 ignored `PRAGMA foreign_keys = OFF`. This fix ensures that the transaction (if exists) is now temporarily committed before `disable_referential_integrity` runs for the `PRAGMA` to take effect. Fixes rails#55866
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
This Pull Request has been created because #55866.
Detail
Fix SQLite3 data loss during table alterations with CASCADE foreign keys
When altering a table in SQLite3 that is referenced by child tables with
ON DELETE CASCADEforeign keys, ActiveRecord would silently delete all data from the child tables. This occurred because SQLite requires table recreation for schema changes, and during this process:This issue could result in catastrophic production data loss when adding columns, renaming tables, or adding foreign keys to tables that are referenced by other tables with CASCADE constraints.
The fix simply reverses the order to follow the official SQLite 12-step ALTER TABLE procedure.
This Pull Request fixes #55866.
Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]