prisma / prisma/orm

Allow to disable foreign key constraint for some relations

Open
#15,744 6 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/feature topic: foreign keys topic: relations
Dominant language
TypeScript
Stars
47.6k
Forks
2.5k
Avg merge
21h 59m
Merged PRs (30d)
95

Description

Problem

I'm using Prisma with PostgreSQL. In out data model, dangling relations are allowed. We import data that may contain relations to other entities that do not exist in the database, but might be added later on. This is not possible with prisma, because all relations have a REFERENCES foreign key constraint forced.

Suggested solution

An optional attribute or argument to the relation attribute that disables the foreign key constraint. Everything else works as it does currently. But you can add rows with values in the scalar column of a reference that do not exist in the referenced table. When joining or querying, NULL would be returned instead.

Alternatives

None I know of apart from manually deleting the constraints with raw SQL after each migration.

Additional context

In #8108 someone mentioned that for MySQL, foreign key constraints are disabled when switching to the Planetscale driver. However, this does not work of course when using PostgreSQL.

Contributor guide

Open the contributing guide

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.

Research direction

Start by reviewing Prisma relation attributes and the migration path that emits PostgreSQL REFERENCES constraints. Compare the requested optional relation setting with current PostgreSQL behavior and the MySQL/Planetscale context in #8108. Done means the behavior and scope of disabling selected constraints are defined and covered across relation handling and migrations.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.