Hi everyone, good day. Please I am getting a `For...
# orm-help
n
Hi everyone, good day. Please I am getting a
Foreign key constraint failed on the field: transactions_senderId_fkey (index)
When I try to insert. And I have no idea why. There shouldn't be on that field. Here's my model
Copy code
model user {
  id         Int      @id @default(autoincrement())
  email      String   @unique
  password   String
  tokens     String[]
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt()
  account    account?
}

model account {
  accNumber Int            @id @default(autoincrement())
  balance   Float
  user      user           @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  userId    Int            @unique
  Sender    transactions[] @relation("sender")
  Receiver  transactions[] @relation("receiver")

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt()
}

model transactions {
  id         Int     @id @default(autoincrement())
  txRef      String  @unique
  amount     Float
  refundRef  String? @unique // this will reference a txRef column of a transaction whenn it is a refund

  from       account @relation("sender", fields: [senderId], references: [accNumber])
  senderId   Int

  to         account @relation("receiver", fields: [receiverId], references: [accNumber])
  receiverId Int

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt()
}
Please can anyone see what's wrong with the model?
@Nurul hi , thanks for the help. But see the problem I'm facing. Any idea why that field has a unique constraint on senderId , meanwhile I have never gotten that error on receiverId . Your help will be greatly appreciated
👀 1
n
Can you share the insert query which you are trying to execute?
Are you passing
from
when invoking
prisma.transactions.create
?
n
@Nurul yes I am, I'm running in tests and in a beforeEach I wipe the db and create new users with their account and two new transactions between the users .
Here's my query beforeEach(async () => { //jest.resetModules(); userOne = { email: 'babs@gmail.com', password: 'nathaniel', account: { create: { balance: 100000.00 } } } userTwo = { email: 'babalola@gmail.com', password: 'nathaniel', account: { create: { balance: 100000.00 } } } const txDelete = prisma.transactions.deleteMany(); const accDelete = prisma.account.deleteMany(); const userDelete = prisma.user.deleteMany(); const deleteM = await prisma.$transaction([txDelete, accDelete, userDelete]); console.log(deleteM); userOneCreate = await prisma.user.create({ data: userOne, include: { account: true } }); userTwoCreate = await prisma.user.create({ data: userTwo, include: { account: true } }); token = await generateToken(userOneCreate); tx = await prisma.transactions.createMany({ data: [ { amount: 2000, receiverId: userTwoCreate.id, senderId: userOneCreate.id, txRef: nanoid(12) }, { amount: 5000, receiverId: userOneCreate.id, senderId: userTwoCreate.id, txRef: nanoid(12) } ] }) });
n
I think you are passing userId in
receiverId
and
senderId
but your schema is expecting accountNumber of
sender
and
receiver
1
If you want to store userId’s then in your transactions schema
from
and
to
should reference to
user
model and not
account
model.
n
@Nurul damn, that's true. Totally my fault 🤦🏾‍♂️.
Thanks a lot
💯 1
n
No worries! Glad that I could help 👍