Skip to content

LF-5275: Add DB tables for linking expenses to crop varieties and to individual animals or batches#4151

Merged
SayakaOno merged 1 commit into
integrationfrom
LF-5275-add-db-tables-for-linking-expenses-to-crop-varieties-and-to-individual-animals-or-batches
May 11, 2026
Merged

LF-5275: Add DB tables for linking expenses to crop varieties and to individual animals or batches#4151
SayakaOno merged 1 commit into
integrationfrom
LF-5275-add-db-tables-for-linking-expenses-to-crop-varieties-and-to-individual-animals-or-batches

Conversation

@litefarm-pr-bot
Copy link
Copy Markdown
Collaborator

@litefarm-pr-bot litefarm-pr-bot commented Apr 28, 2026

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_variety uses 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 an allocated_value FLOAT NOT NULL for the portion of the expense assigned to that variety.

farm_expense_animal uses a surrogate auto-increment primary key (matching the animal_sale pattern) because the XOR constraint between animal_id and animal_batch_id cannot 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 an allocated_value FLOAT NOT NULL.

Both tables reference farmExpense(farm_expense_id) but do NOT use ON DELETE CASCADE — the existing expense soft-delete pattern sets deleted = true on 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

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:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have ordered translation keys alphabetically (optional: run pnpm i18n to help with this)
  • I have added the GNU General Public License to all new files

…tion tables

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@litefarm-pr-bot litefarm-pr-bot requested review from a team as code owners April 28, 2026 01:26
@litefarm-pr-bot litefarm-pr-bot requested review from kathyavini and removed request for a team April 28, 2026 01:26
@SayakaOno SayakaOno added this pull request to the merge queue May 11, 2026
Merged via the queue into integration with commit fafbcbf May 11, 2026
4 checks passed
@SayakaOno SayakaOno deleted the LF-5275-add-db-tables-for-linking-expenses-to-crop-varieties-and-to-individual-animals-or-batches branch May 11, 2026 17:58
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