Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select from Model.history, then use .instance, then chase a foreign key is broken #983

Closed
jeking3 opened this issue May 5, 2022 · 2 comments · Fixed by #984
Closed

Select from Model.history, then use .instance, then chase a foreign key is broken #983

jeking3 opened this issue May 5, 2022 · 2 comments · Fixed by #984
Assignees
Labels
bug Issues related to confirmed bugs

Comments

@jeking3
Copy link
Contributor

jeking3 commented May 5, 2022

Describe the bug
A clear and concise description of what the bug is.

In 3.1 HistoricForeignKey was introduced which is intended to work with Model.history.as_of(timepoint) and this works.

In 3.1.1, calling .instance on a historic record lookup does not populate _as_of which is necessary for HistoricForeignKey to work. You can query history with as_of(timepoint) and things work properly, but querying Model.history directly then converting to an instance cannot chase the foreign keys properly.

My workaround in another project:

            if isinstance(value, Value.history.model):
                # work around https://github.com/jazzband/django-simple-history/issues/983
                # queries beginning with as_of properly inject, but direct selections
                # converted to instances do not... :|  I missed something!!
                setattr(value, "_as_of", value.modified_at)
                # then value.instance properly works
@jeking3 jeking3 added the bug Issues related to confirmed bugs label May 5, 2022
@jeking3 jeking3 self-assigned this May 5, 2022
@jeking3
Copy link
Contributor Author

jeking3 commented May 6, 2022

So I'm thinking the Historical model MAY have enough information in it to work without the extra attribute but I will have to play with it.

@jeking3
Copy link
Contributor Author

jeking3 commented May 7, 2022

In the end, if you did not query history with as_of, and you end up with a historical instance, if you call .instance on it, the HistoricalForeignKey chasers will use a fallback of the history_date of the record that made the instance when chasing relations! This was an easy fix and an easy win, and makes django-simple-history more powerful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues related to confirmed bugs
Projects
None yet
1 participant