Models: composite primary keys
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
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, withfindandfind_manytaking a tuple:Product::find((42, tenant_id)). update,delete,refresh,freshandismatching on every key column.chunk_by_idcould 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust-framework
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
laravel/rust-framework#876 ·
Maintainers usually reply within 1 day
-
Case-sensitive contains, starts_with and ends_with that match % and _ literally on every databaseOpen
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#874 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
laravel/rust-framework#873 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#872 ·
Maintainers usually reply within 1 day
All issues in laravel/rust-framework
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days