Skip to content

Cannot use @relation(id) to specify the side of the relation #185

Closed

Description

@Errorname

The examples for lift shows the following:

// The @relation attribute can be used to specify the side of the foreign key.
// Here, it's explicitly stored on the `User` model.
model User {
  id        Int           @id
  customer  Customer?     @relation(id) // stores a foreign key
  name      String
}

model Customer {
  id       Int     @id
  user     User?
  address  String
}

However, when trying it with the following schema:

datasource db {
  provider = "sqlite"
  url      = "file:dev.db"
  default  = true
}

generator photon {
  provider = "photonjs"
}

model B {
  id String @default(cuid()) @id
  a  A?     @relation(id, name: "BtoA")
  c  C?     @relation(id, name: "BtoC")
}

model A {
  id String @default(cuid()) @id
  b  B?
}

model C {
  id String @default(cuid()) @id
  b  B?
}

And then opening the generated dev.db file and exporting its schema, we get the following:

CREATE TABLE "A"("id" TEXT NOT NULL  ,PRIMARY KEY ("id"));
CREATE TABLE "B"("id" TEXT NOT NULL  ,"a" TEXT   REFERENCES A(id),PRIMARY KEY ("id"));
CREATE TABLE "C"("id" TEXT NOT NULL  ,"b" TEXT   REFERENCES B(id),PRIMARY KEY ("id"));
...

This shows that B has the foreign key of A, but it does not have the foreign key of C. In the contrary, it's C that has the foreign key of B.

Activity

  1. pantharshit00 commented on Jul 19, 2019

    @pantharshit00
    Contributor

    You will need to do the following:

    datasource db {
      provider = "sqlite"
      url      = "file:dev.db"
      default  = true
    }
    
    generator photon {
      provider = "photonjs"
    }
    
    model B {
      id String @default(cuid()) @id
      a  A?     @relation(references: [id], name: "BtoA")
      c  C?     @relation(references: [id], name: "BtoC")
    }
    
    model A {
      id String @default(cuid()) @id
      b  B?
    }
    
    model C {
      id String @default(cuid()) @id
      b  B?
    }
    

    Can you please share the link the to example?

  2. Errorname commented on Jul 19, 2019

    @Errorname
    ContributorAuthor

    Ah, thanks for the answer, so that means it's just an inconsistency between the documentation and the example.

    The example can be found directly on the https://lift.prisma.io/ website, when clicking on the "One to One" button

  3. added this to the Preview 9 milestone on Aug 23, 2019
  4. nikolasburk commented on Aug 26, 2019

    @nikolasburk
    Contributor

    Thanks a lot for opening this issue @Errorname 🙌 I've just updated the example code on the Lift website and also clarified this in the actual documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions