Raise error if required association not loaded in preload#812
Merged
matthewmcgarvey merged 2 commits intoluckyframework:mainfrom Feb 15, 2022
Merged
Conversation
matthewmcgarvey
commented
Feb 15, 2022
|
|
||
| class MissingRequiredAssociationError < AvramError | ||
| def initialize(model : Avram::Model.class, association : Avram::Model.class) | ||
| super "Expected #{model} to have an association with #{association} but one was not found." |
Member
Author
Contributor
There was a problem hiding this comment.
How about Expected Comment to be assigned to a Post but no association was supplied?
Or Expected Comment to belong to a Post but no Post was supplied
Member
Author
There was a problem hiding this comment.
Whoops, forgot to wait for feedback on this 😬 Feel free to open a PR with an error message that you think is better. Otherwise I might remember to make one at some point
Contributor
There was a problem hiding this comment.
Well. I could probably have also looked to see if it was merged before trying to be helpful...
Member
|
OMG 🤣 I should have looked at this before writing my previous comment on the issue... |
jwoertink
approved these changes
Feb 15, 2022
Member
jwoertink
left a comment
There was a problem hiding this comment.
Ok, this wasn't exactly what I was thinking, but I think this is still pretty good 👍
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.

Related #672
If a model defines a required belongs to association and the record isn't retrieved in a preload, this raises an appropriate error. Also added for required has one associations.
Applying this to the related issue, in the scenario where you have soft deletes, the solution now becomes making what was a required association optional since it might not always be loaded. This doesn't change whether or not it is loaded but will guarantee an error unless you change the association. Before, the error only occurred if you tried to access the association on the particular record.