Hello, I’m having trouble modeling a pair in my sc...
# prisma-client
e
Hello, I’m having trouble modeling a pair in my schema. e.g. I’m trying to model
Pair
being a unique pair of
Tokens
— so far my schema is:
Copy code
model Token {
  id      Int    @id @default(autoincrement())
  chainId Int
  address String
  name    String

  @@unique([chainId, name])
}

model Pair {
  id                  Int                   @id @default(autoincrement())
  chainId             Int
  name                String
  token0Id            Int
  token1Id            Int

  @@unique([chainId, name])
}
Any hints on how to do this?
1
r
Hi @Eric Tsang 👋, Please could you clarify if a pair will have 0 or 1 tokens or if it will have multiple tokens. Based on that information, we can safely model both entities. Also, looking at the code snippet you shared, I would update the token property in the pair model to have a type of Token or Token[ ] depending on if it’s a one to one or one to many relationship.
Copy code
model Token {
  id             Int    @id @default(autoincrement())
  chainId        Int
  address        String
  name           String
  pairsAsToken0 Pair[] @relation("token0")
  pairsAsToken1 Pair[] @relation("token1")

  @@unique([chainId, name])
}

model Pair {
  id       Int    @id @default(autoincrement())
  chainId  Int
  name     String
  token0   Token  @relation("token0", fields: [token0Id], references: [id])
  token1   Token  @relation("token1", fields: [token1Id], references: [id])
  token0Id Int
  token1Id Int

  @@unique([chainId, name])
}
e
Hi @Raphael Etim a Pair should have 2 different tokens. Once set, it shouldn't change.
r
Hi @Eric Tsang, apologies for the delay in following up, did the model i shared work for your usecase.
e
I ended up using my own. Hadn't touched it since :)
👍 1