prismix icon indicating copy to clipboard operation
prismix copied to clipboard

Enhancement: Model constraint in associated model

Open sudhons opened this issue 4 years ago • 2 comments

This is a good solution for the Prisma single file issue. 👍🏼

I have Shop, User and Cart models. And the shop.prisma file is as below:

model Shop {
  id            String       @id @default(cuid())
  name          String
  customerCarts Cart[]
}

model Cart {
  id     String @id @default(cuid())
  shopId String
  userId String
  shop   Shop   @relation(fields: [shopId], references: [id])

  @@unique([shopId, userId])
}

Now @@unique([shopId, userId]) is a constraint on the Cart table. If that line is left out (as below) the generated schema won't have it.

model Cart {
  id     String @id @default(cuid())
  shopId String
  shop   Shop   @relation(fields: [shopId], references: [id])
}

Because of this, every model associated with Cart has to have this @@unique([shopId, userId]) spelt out. For example, my User model in user.prisma have to be

model User {
  id          String       @id @default(cuid())
  firstname   String
  lastname    String
  email       String       @unique
  password    String
  carts       Cart[]
}

model Cart {
  id     String @id @default(cuid())
  shopId String
  userId String
  user   User   @relation(fields: [userId], references: [id])

  @@unique([shopId, userId])
}

Since @@unique([shopId, userId]) isn't used as a foreign key. It really won't be easier if it could be left out in all associated models and put only in the cart.prisma file.

sudhons avatar Jul 31 '21 16:07 sudhons

I am not able to replicate this bug at all... Would you be able to provide a repo where i can take a look? I have two files as follows

cart.prisma

model Shop {
  id            String       @id @default(cuid())
  name          String
  customerCarts Cart[]
}

model Cart {
  id     String @id @default(cuid())
  shopId String
  userId String
  shop   Shop   @relation(fields: [shopId], references: [id])

  @@unique([shopId, userId])
}

user.prisma

model User {
  id          String       @id @default(cuid())
  firstname   String
  lastname    String
  email       String       @unique
  password    String
  carts       Cart[]
}

model Cart {
  id     String @id @default(cuid())
  shopId String
  userId String
  user   User   @relation(fields: [userId], references: [id])
}

and the generated file as follows

// *** GENERATED BY PRISMIX :: DO NOT EDIT ***
model Shop {
	id String @id @default(cuid())
	name String 
	customerCarts Cart[] @relation(name: "CartToShop")
}
model Cart {
	id String @id @default(cuid())
	shopId String 
	userId String 
	shop Shop @relation(name: "CartToShop", fields: [shopId], references: [id])
	user User @relation(name: "CartToUser", fields: [userId], references: [id])
	@@unique([shopId, userId])
}
model User {
	id String @id @default(cuid())
	firstname String 
	lastname String 
	email String @unique
	password String 
	carts Cart[] @relation(name: "CartToUser")
}

sean-brydon avatar Sep 09 '21 08:09 sean-brydon

I ran into this issue as well, but I believe I fixed it in #20. The underlying issue was that new models replaced old ones, so whether or not your @@unique appeared depended on the order in which different models were processed.

ddhorstman avatar Feb 18 '22 14:02 ddhorstman