Insert operations of models with non-PK foreign keys fail when passing a target object instance #2408
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.
I have a model
A
with aForeignKeyField
column that uses thefield
parameter to set a non-pk reference column in the target model:When trying to create instances of
A
, I can do so withcreate()
, but not with theModelInsert
-based methods:In the last case, the generated SQL uses the primary key of
t
instead oft.key
because the code path inModelInsert
does not apply thefield
parameter of theForeignKeyField
to the passed-in model instance. The attached patch fixes the problem for me.