Skip to content

One-To-One relationship between Farm and Project#652

Merged
vincent-olivert-riera merged 3 commits intoline:masterfrom
hoangpn:feature/restructure_farm
Sep 30, 2025
Merged

One-To-One relationship between Farm and Project#652
vincent-olivert-riera merged 3 commits intoline:masterfrom
hoangpn:feature/restructure_farm

Conversation

@hoangpn
Copy link
Contributor

@hoangpn hoangpn commented Sep 26, 2025

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:

  • Migration scrips for removing project.farm field, adding farm.project field and migrating existing data.
  • Code changes to suit with new the relationship. (For example: farm.project_set becomes farm.project,...)

@hoangpn hoangpn force-pushed the feature/restructure_farm branch 2 times, most recently from 3169764 to cf26010 Compare September 26, 2025 03:30
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/
@hoangpn hoangpn force-pushed the feature/restructure_farm branch 3 times, most recently from 3feaa4d to e97f7f9 Compare September 29, 2025 07:21
@hoangpn hoangpn changed the title [WIP] One-To-One relationship between Farm and Project One-To-One relationship between Farm and Project Sep 29, 2025
@hoangpn hoangpn force-pushed the feature/restructure_farm branch 3 times, most recently from 4f92b77 to 3213819 Compare September 29, 2025 14:30
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.
Copy link
Contributor

@vincent-olivert-riera vincent-olivert-riera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vincent-olivert-riera vincent-olivert-riera merged commit b8094b3 into line:master Sep 30, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants