One-To-One relationship between Farm and Project#652
Merged
vincent-olivert-riera merged 3 commits intoline:masterfrom Sep 30, 2025
Merged
One-To-One relationship between Farm and Project#652vincent-olivert-riera merged 3 commits intoline:masterfrom
vincent-olivert-riera merged 3 commits intoline:masterfrom
Conversation
3169764 to
cf26010
Compare
We have planned to make Farm a child object of Project. Each Project will have its own Farm and will not share it with other Projects. This means the real relationship between a Project and a Farm will be one-to-one. Therefore, we should use Django's one-to-one relationship to implement the relationship between the Farm and Project models. In the official Django documentation, one-to-one relationships are asymmetrical, and the definition of the parent should be placed in the child. However, we have currently defined "farm" as a field of the Project model. Therefore, we will switch the direction by adding a "project" field to the Farm model, populate data into the promgen_farm.project_id column, and then remove the "farm" field in the Project model. Before removing the "farm" field, both definitions will have related_name="+" so that the reverse relationships do not clash with the other field. Ref: https://docs.djangoproject.com/en/4.2/topics/db/examples/one_to_one/
3feaa4d to
e97f7f9
Compare
4f92b77 to
3213819
Compare
vincent-olivert-riera
requested changes
Sep 30, 2025
After successfully creating "project" field in the Farm model and populating all the necessary existed data, the "farm" field of the Project model is redundant and safe to remove because the relationship is one-to-one. Therefore, we have removed this field.
Due to changing relationship between Farm and Project to one-to-one, the way we access the parent Project from a Farm will be "farm.project" instead of "farm.project_set". We have modified all necessary code changes correspond to this. A signal also became redundant and has been removed because we have defined on_delete=models.CASCADE in the Farm model class.
3213819 to
0fe787d
Compare
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.
Make the relationship between Farm and Project become One-To-One from Many-To-One by using Django's OneToOneField. The dependency also have been reversed by removing project.farm and adding farm.project.
This PR includes: