Skip to content

Relations Foreign Key Reference #82

Closed

Description

@HexSleeves

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"))

Activity

  1. HexSleeves commented on Jul 3, 2019

    @HexSleeves
    Author

    It also seems that @relation(onDelete: CASCADE) is not functioning as well. Deleting a provider should delete the fellow user, but the user referencing the deleted provider is not deleted

  2. added
    bug/2-confirmed
    kind/bug
    on Jul 3, 2019
  3. pantharshit00 commented on Jul 3, 2019

    @pantharshit00
    Contributor

    I can confirm this.

    Right now we are creating the foreign key on the model which appears first in the project.prisma file. But it should be in accordance with lexicographical precedence.

    Also, onDelete: CASCADE is yet to be implemented.

  4. HexSleeves commented on Jul 3, 2019

    @HexSleeves
    Author

    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"));
  5. pantharshit00 commented on Jul 9, 2019

    @pantharshit00
    Contributor

    Ref: graphql-nexus/nexus-prisma#328

    One more thing we want to keep in mind is creating a foreign key for an optional relation defined in the datamodel.

    For example:

    model User {
      id         String        @default(cuid()) @id @unique
      email      String        @unique
      staff      Staff?
    }
    
    model Staff {
      id        String   @default(cuid()) @id @unique
      user      User
      active    Boolean?
    }

    If we create the database schema for the above datamodel using Lift, it will create a foreign key on the User field. But in the datamodel, Staff on a User is optional but a User is required on the Staff.

    So when we try to create a User:

    const user: User = await photon.users.create({
    	data:{
    		email: "[email protected]"
    	}
    });

    It will throw a Null Constraint violation exception as shown below:

    "error": "ConnectorError(NullConstraintViolation { field_name: \"User.staff\" })"

    This shouldn't be the case. This is also easily fixable by creating the foreign key on staff rather than on the user.

    model Staff {
      id     String   @default(cuid()) @id @unique
      user   User     @relation(references: [id])
      active Boolean?
    }
    
    model User {
      id    String @default(cuid()) @id @unique
      email String @unique
      staff Staff?
    }
  6. mavilein commented on Jul 31, 2019

    @mavilein
    Contributor

    This is fixed. It will ship with preview4.

  7. added this to the Preview 4 milestone on Aug 9, 2019
  8. tomitrescak commented on Oct 22, 2019

    @tomitrescak
    Contributor

    Hi, the CASCADE does not work though ;(

  9. janpio commented on Oct 22, 2019

    @janpio
    Contributor

    Can you please open a new issue about this @tomitrescak? We will then look into it with the necessary information you provide about the problem. Thanks.

    Update: I see you already did that in #810 - thanks!

  10. added 2 commits that reference this issue on Apr 14, 2020
    d1ff71e
    e29932e
  11. added a commit that references this issue on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions