Relations Foreign Key Reference
See original GitHub issueUsing 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:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is fixed. It will ship with
preview4
.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
Creates
Example 2 - userRef < userRe Model
Creates