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.

Relations Foreign Key Reference

See original GitHub issue

Using the following model:

model User {
  id        String    @default(cuid()) @id @unique
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  email     String    @unique
  name      String?
  slack     Provider? @relation(onDelete: CASCADE)
}

model Provider {
  id           String  @default(cuid()) @id @unique
  userId       String  @unique
  accessToken  String  @unique
  refreshToken String? @unique
  user         User?   @relation(onDelete: CASCADE)
}

Reading the docs… “For 1:1 relationships, it doesn't matter on which side you store the foreign key. Prisma has a convention that the foreign key is added to the model which appears first alphanumerically in your data model”.

But running SELECT * FROM sqlite_master; shows me differently

CREATE TABLE "Provider"("id" TEXT NOT NULL  ,"userId" TEXT NOT NULL DEFAULT '' ,"accessToken" TEXT NOT NULL DEFAULT '' ,"refreshToken" TEXT   ,PRIMARY KEY ("id"))
CREATE TABLE "User"("id" TEXT NOT NULL  ,"createdAt" DATE NOT NULL  ,"updatedAt" DATE NOT NULL DEFAULT '1970-01-01 00:00:00' ,"email" TEXT NOT NULL DEFAULT '' ,"name" TEXT   ,"slack" TEXT NOT NULL  REFERENCES Provider(id),PRIMARY KEY ("id"))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mavileincommented, Jul 31, 2019

This is fixed. It will ship with preview4.

1reaction
lecoqjacobcommented, Jul 3, 2019

After some testing, it seems the foreign key is determined by which referenced field on the two referencing models comes with lexicographical precedence.

Example 1 - userRef > userReff Model

model User2 {
  id      String @default(cuid()) @id @unique
  userRef User3
}

model User3 {
  id      String @default(cuid()) @id @unique
  userReff User2
}

Creates

CREATE TABLE "file:dev"."User2"("id" TEXT NOT NULL  ,"userRef" TEXT NOT NULL  REFERENCES User3(id),PRIMARY KEY ("id"));

CREATE TABLE "file:dev"."User3"("id" TEXT NOT NULL  ,PRIMARY KEY ("id"));

Example 2 - userRef < userRe Model

model User2 {
  id      String @default(cuid()) @id @unique
  userRef User3
}

model User3 {
  id      String @default(cuid()) @id @unique
  userRe User2
}

Creates

CREATE TABLE "file:dev"."User2"("id" TEXT NOT NULL  ,PRIMARY KEY ("id"));

CREATE TABLE "file:dev"."User3"("id" TEXT NOT NULL  ,"userRe" TEXT NOT NULL  REFERENCES User2(id),PRIMARY KEY ("id"));
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a foreign key? (With SQL examples) - Cockroach Labs
A foreign key column in a table points to a column with unique values in another table (often the primary key column) to...
Read more >
Can a foreign key reference another foreign key - Stack Overflow
A foreign key can reference any field defined as unique. If that unique field is itself defined as a foreign key, it makes...
Read more >
Create Foreign Key Relationships - SQL Server | Microsoft Learn
FOREIGN KEY constraints can reference another column in the same table, and is referred to as a self-reference. A FOREIGN KEY constraint ...
Read more >
What is a database foreign Key? A beginner's tutorial
A foreign key is a column (or group of columns) used in a relational database to link data between tables. A foreign key...
Read more >
Foreign key - Wikipedia
The foreign key links these two tables. ... In simpler words, a foreign key is a set of attributes that references a candidate...
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 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