You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support multi-column (composite) foreign keys in model relations #5566
A relation can only ever produce a single-column foreign key. create_definition.dart:118 builds columns: [field.columnName], so a composite foreign key cannot be expressed no matter how the model is arranged.
This is not only a code-generation limit. Because schema validation requires every live foreign key to be declared in the model, a table that has a composite foreign key in the database cannot become a Serverpod model at all — even with managedMigration: false, and even if nothing else about it is unusual.
4.0.0-beta.1
Proposal
Allow a relation to declare more than one column pair, e.g.
Failing full support, being able to declare the constraint for validation purposes — without the ORM traversing it as a relation — would be enough to unblock adoption.
A self-referencing composite foreign key enforcing that a child's parent lives in the same site. This is a standard relational technique for keeping a tenant/scope key consistent down a hierarchy — the referenced (id, site_id) unique constraint exists purely to support it.
The invariant is enforced by the database today. Migrating this table to the ORM would mean moving it into Dart, where a concurrent write can violate it.
Hi @hamaney! This is something we could potentially support, but it has a fairly large prerequisite.
Before supporting multi-column foreign keys, we would first need to support multi-column primary keys in Serverpod. That is a significant architectural change, since the assumption of a single id column is deeply embedded throughout the codebase, including code generation, the ORM, relations, and migrations.
So this issue is definitely valid, but implementing it must come after (or as part of) a broader effort to support composite primary keys.
Another use case for this, plus a note on the stated prerequisite.
The pattern we need it for is tenant scoping in a multi-tenant schema:
-- on the parent (already expressible in the model today)
UNIQUE ("tenantId", "id")
-- on the child (not expressible)FOREIGN KEY ("tenantId", "parentId") REFERENCES"parent" ("tenantId", "id")
A single-column parentId -> parent.id reference proves the parent row exists. The composite form additionally proves it belongs to the same tenant, so a cross-tenant reference becomes impossible in the database rather than something application code has to remember to prevent.
On @marcelomendoncasoares' point that multi-column primary keys would be needed first: that seems right for full support, where the ORM traverses the relation. I do not think it is needed for the fallback this issue already proposes — declaring the constraint for code generation and validation only, with no ORM traversal.
In this pattern the parent keeps an ordinary single-column id primary key. The composite foreign-key target is a plain UNIQUE ("tenantId", "id") index, which PostgreSQL accepts as a foreign-key target and which the model can already declare today. So:
no composite primary key is required,
nothing needs to be navigable from Dart,
the only gap is the ability to state "this constraint exists", so generation emits it and validation stops reporting it as drift.
If a declare-only form were acceptable, it would sidestep the architectural prerequisite entirely and still solve the practical problem for schemas that use this pattern. The alternative for us is #4743 (marking externally-owned objects so validation ignores them), which fixes the validation half but leaves the constraint to be created and maintained by hand.
One thing for whoever picks this up: the PostgreSQL dialect currently mis-quotes multi-column source columns, so composite definitions would emit invalid DDL as things stand. Filed separately as #5735.
Activity
marcelomendoncasoares commentedon Sep 17, 2026
Hi @hamaney! This is something we could potentially support, but it has a fairly large prerequisite.
Before supporting multi-column foreign keys, we would first need to support multi-column primary keys in Serverpod. That is a significant architectural change, since the assumption of a single
idcolumn is deeply embedded throughout the codebase, including code generation, the ORM, relations, and migrations.So this issue is definitely valid, but implementing it must come after (or as part of) a broader effort to support composite primary keys.
stohal commentedon Sep 19, 2026
Another use case for this, plus a note on the stated prerequisite.
The pattern we need it for is tenant scoping in a multi-tenant schema:
A single-column
parentId -> parent.idreference proves the parent row exists. The composite form additionally proves it belongs to the same tenant, so a cross-tenant reference becomes impossible in the database rather than something application code has to remember to prevent.On @marcelomendoncasoares' point that multi-column primary keys would be needed first: that seems right for full support, where the ORM traverses the relation. I do not think it is needed for the fallback this issue already proposes — declaring the constraint for code generation and validation only, with no ORM traversal.
In this pattern the parent keeps an ordinary single-column
idprimary key. The composite foreign-key target is a plainUNIQUE ("tenantId", "id")index, which PostgreSQL accepts as a foreign-key target and which the model can already declare today. So:If a declare-only form were acceptable, it would sidestep the architectural prerequisite entirely and still solve the practical problem for schemas that use this pattern. The alternative for us is #4743 (marking externally-owned objects so validation ignores them), which fixes the validation half but leaves the constraint to be created and maintained by hand.
One thing for whoever picks this up: the PostgreSQL dialect currently mis-quotes multi-column source columns, so composite definitions would emit invalid DDL as things stand. Filed separately as #5735.