Models: composite primary keys

Open
#499 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend, database

Research direction

Start with macros/model/src/model.rs at the reported compile error, then trace how model keys are used by find, write operations, relationships, eager loading, and route model binding. The issue describes the required behavior across these areas; done means composite keys work consistently, generated keys are unavailable for them, and route binding either supports them or gives the specified compile error.

Written by the indexing model from the issue text.

Description

Problem

Legacy and multi-tenant schemas often identify a row by two columns. One example is ids numbered per tenant with PRIMARY KEY (id, tenant_id). Such a table can't be a model today: a second #[key] field fails to compile with "a model has one key; mark only one field #[key]" (macros/model/src/model.rs#L1487). Marking only id as the key does compile, but then update, delete and refresh match on id alone, so they reach that id's row in every tenant.

The schema builder can already create composite foreign keys (foreign), so migrations can describe these tables. Only models and relationships can't use them.

What would cover it

  • Several #[key] fields, with find and find_many taking a tuple: Product::find((42, tenant_id)).
  • update, delete, refresh, fresh and is matching on every key column. chunk_by_id could require a single key, or order by the whole tuple.
  • Relationships with multi-column keys, including eager loading with with: #[belongs_to(foreign_key = ["product_id", "tenant_id"], owner_key = ["id", "tenant_id"])].
  • Route model binding for these models either from several segments, or not at all with a compile error that says so.
  • No generated keys: composite keys are always supplied on create.

Workaround today

db::table or db::query! with WHERE id = {id} AND tenant_id = {tenant} written by hand everywhere. That gives up typed columns, relationships and with for these tables.

🤖 Generated with Claude Code

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.