Skip to content

Support multi-column (composite) foreign keys in model relations #5566

Open

Description

@hamaney

Problem to Solve

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.

parent:
  relation:
    parent: site_components
    fields: parentId, siteId
    references: id, siteId
    onDelete: Cascade

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.

Use Case

CONSTRAINT site_components_parent_same_site_fkey
  FOREIGN KEY (parent_id, site_id)
  REFERENCES site_components (id, site_id)
  ON DELETE CASCADE

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.

Alternatives

4.0.0-beta.1

Additional context

4.0.0-beta.1

How experienced are you with this library?

Beginner - Just getting started with this library

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    enhancement
    on Aug 8, 2026
  2. marcelomendoncasoares commented on Sep 17, 2026

    @marcelomendoncasoares
    Collaborator

    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.

  3. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Sep 17, 2026
  4. stohal commented on Sep 19, 2026

    @stohal

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions