Is there a way to set constraints on a model? Giv...
# orm-help
a
Is there a way to set constraints on a model? Given the following model
Copy code
model Follows {
  createdAt   DateTime @default(now())
  follower    User     @relation("follower", fields: [followerId], references: [id])
  followerId  Int
  following   User     @relation("following", fields: [followingId], references: [id])
  followingId Int

  @@id([followerId, followingId])
}
I want make sure followerId and followingId can never be the same.
r
@Awey 👋 You cannot set them on the model at the moment, but you can set them in your migration files.
It would be great if you could add a 👍 to this request so that we can know the priority 🙂
a
@Ryan So edit the migration file that was just generated?
20211116063937_init/migration.sql
r
No that would already have been applied. You would ideally do this in the following steps: 1. Create a migration using
prisma migrate dev --create-only
2. Edit the generated
.sql
file and add the constraint 3. Apply the migration using
prisma migrate dev
🙌 1
a
Copy code
ALTER TABLE follows
ADD CHECK (follower_id <> following_id);
Works perfectly, thanks for the help @Ryan
🙌 1