question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

1-n many relation works implicitly without specifying the foreign key

See original GitHub issue

I’m not sure we would consider this a bug but it certainly seems a bit strange that this is functional so I wanted to raise it here on GitHub. The following schema creates a correct 1-n relation in the DB without the foreign key being specified in the Prisma schema:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["nativeTypes"]
}

datasource db {
  provider = "postgresql"
  url      = "postgresql://nikolasburk:nikolasburk@localhost:5432/testdb1"
}

model user {
  id             String        @id @default(uuid()) @db.Uuid()
  email          String        @unique
  password       String
  createdAt      DateTime      @default(now()) @map("created_at")
  organization   organization? @relation(references: [id])
}
model organization {
  id        String   @id @default(uuid()) @db.Uuid()
  name      String
  createdAt DateTime @default(now()) @map("created_at")
  users     user[]
}

Running prisma migrate dev creates the following SQL:

-- CreateTable
CREATE TABLE "user" (
    "id" UUID NOT NULL,
    "email" TEXT NOT NULL,
    "first_name" TEXT NOT NULL,
    "last_name" TEXT,
    "password" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "organizationId" UUID,

    PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "organization" (
    "id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "user.email_unique" ON "user"("email");

-- AddForeignKey
ALTER TABLE "user" ADD FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE SET NULL ON UPDATE CASCADE;

My understanding is that foreign keys should be made explicit in the Prisma schema and specified via references inside the @relation attribute. If the case above should be allowed, we probably should document this approach too.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pantharshit00commented, Jan 29, 2021

I would also say this schema should be rejected by that validator. I have candidated this issue so that we can have a look into this in the next planning.

0reactions
do4grcommented, Feb 25, 2021

This should be solved by us splitting the parsing and formatting step here https://github.com/prisma/prisma-engines/pull/1662

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql server - Why is it necessary to explicitly specify the foreign ...
SQL server is aware of table dependencies based on foreign keys, so why is it necessary to explicitly specify a JOIN ON foreign...
Read more >
SQL Server: Can you join implicitly using key relationships?
Yes, you still have to explicitly declare the join columns. Share. Share a link to this answer.
Read more >
Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >
Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >
Many-to-many relations - Prisma
Although the relation table exists in the underlying database, it is managed by Prisma and does not manifest in the Prisma schema. Implicit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found