LF-5275: Add DB tables for linking expenses to crop varieties and to individual animals or batches#4151
Merged
Conversation
…tion tables Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
SayakaOno
approved these changes
May 11, 2026
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.
Description
Expense items currently have no way to record which crops or animals they relate to. This PR creates two junction tables that will back the optional entity association section being added to the expense forms.
farm_expense_crop_varietyuses a composite primary key(farm_expense_id, crop_variety_id), which structurally enforces that a single expense cannot allocate to the same crop variety twice. Each row carries anallocated_value FLOAT NOT NULLfor the portion of the expense assigned to that variety.farm_expense_animaluses a surrogate auto-increment primary key (matching theanimal_salepattern) because the XOR constraint betweenanimal_idandanimal_batch_idcannot be expressed as a composite key. A CHECK constraint enforces that exactly one of the two nullable FK columns is non-null on each row. Each row also carries anallocated_value FLOAT NOT NULL.Both tables reference
farmExpense(farm_expense_id)but do NOT useON DELETE CASCADE— the existing expense soft-delete pattern setsdeleted = trueon the parent without physically removing rows, so cascade would fire erroneously. The allocation rows remain until the parent expense is physically deleted (which does not happen in the current workflow).Jira link: https://lite-farm.atlassian.net/browse/LF-5275
Type of change
How Has This Been Tested?
Migration reviewed in source code. No local DB running; migration was not executed locally. Backend model and controller changes that depend on this schema ship in a follow-up PR.
Checklist:
pnpm i18nto help with this)