Include association keys when importing models with explicit columns - #905
Open
OskarEichler wants to merge 1 commit into
Open
Include association keys when importing models with explicit columns#905OskarEichler wants to merge 1 commit into
OskarEichler wants to merge 1 commit into
Conversation
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.
Summary
When importing model instances through a collection with an explicit column list, check that selected list when adding the owner foreign key. The old code checked every schema column instead, so it omitted the foreign key from the INSERT even though the in-memory model had it.
Also include the polymorphic type column when required, matching the existing hash/value import paths.
Reproduction
For
AuditParent has_many :audit_children:Before: nil. After: the parent ID. The same failure/fix is reproduced with string column names.
For
has_many :attachments, class_name: 'AuditChild', as: :attachable, importing[:name]previously leaves bothattachable_idandattachable_typenil; afterward both identify the owner.Focused checks also cover already-included string/symbol foreign keys, avoiding duplicate columns. Input column arrays remain copied, not mutated. Composite association key support and STI polymorphic naming are outside this patch.
Verification and limitations
Ruby 4.0.6 / Active Record 8.1.3.1. The unchanged existing suites pass both before and after this individual patch: PostgreSQL 15.19: 304 runs, 792 assertions; SQLite 2.2.0: 225 runs, 563 assertions; no failures/errors/skips. Targeted RuboCop, Ruby syntax and whitespace checks pass.
Checks use an isolated PostgreSQL Unix socket and an in-memory SQLite database; no production services. A temporary external verification Gemfile supplies the existing test dependencies, Minitest 5 and Ruby 4's extracted rdoc/ostruct libraries. No tests, gem versions, runtime dependencies or upstream development configuration were changed. The full historical Ruby/database matrix was not run locally.
Breaking changes
None intended. Public signatures and result shapes are unchanged; the behavior correction is described above.
Prepared with Codex assistance; the source change and reproduction were reviewed and executed.