Hi guys. I am having a problem when doing a creat...
# orm-help
r
Hi guys. I am having a problem when doing a create() on a model. It returns the P2014 error. Anyone else had this error before? This is the mutation:
Copy code
async createPointsObject(
    points: number,
    notification: Notification,
    userEmail: string
  ) {
    return await this.prismaService.client.points.create({
      data: {
        points,
        Notification: { connect: { id: Notification.id } },
      },
    });
  }
Here is my schema.
Copy code
model Points {
  id                  String              @id @default(cuid())
  points              Int
  createdAt           DateTime            @default(now())
  Notification        Notification
}

model Notification {
  id           String           @id @default(uuid())
  points       Points           @relation(fields: [pointsId], references: [id])
  pointsId     String
  createdAt    DateTime         @default(now())
  updatedAt    DateTime         @updatedAt
}
Full Error log:
Copy code
[dev        ]   The change you are trying to make would violate the required relation 'NotificationToPoints' between the `Notification` and `Points` models.
.....................................................
[dev        ]   code: 'P2014',
[dev        ]   meta: {
[dev        ]     relation_name: 'NotificationToPoints',
[dev        ]     model_a_name: 'Notification',
[dev        ]     model_b_name: 'Points'
[dev        ]   }
[dev        ] }
r
This should work:
Copy code
model Points {
  id           String       @id @default(cuid())
  points       Int
  createdAt    DateTime     @default(now())
  Notification Notification
}

model Notification {
  id        String   @id @default(uuid())
  points    Points?  @relation(fields: [pointsId], references: [id])
  pointsId  String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}