Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect on create bug #954

Open
bogannathan opened this issue Jun 13, 2024 · 2 comments
Open

Connect on create bug #954

bogannathan opened this issue Jun 13, 2024 · 2 comments
Labels
invalid This doesn't seem right question Further information is requested

Comments

@bogannathan
Copy link

bogannathan commented Jun 13, 2024

Thanks for the great package. I'm mocking out tests for our next app, but I am unable to connect on create with this setup.

I create a user earlier in the test and it seems to work fine. I am able to see the user in the first log below.

export const create_ticket = async (user_id: number, type: TicketType): Promise<Ticket> => {
  return prisma.ticket.create({
    data: {
      type,
      user: { connect: { user_id }}},
    include: {
      user: true
    }
  })
}

......
const user = await prisma.user.findUnique({where: { user_id: 1 }})
  console.log(user)
  
  const res = await create_ticket(1, 'free')
  console.log(res)

  const ticket = await get_ticket_by_id(1)
  console.log(ticket)
{
id: 1,
user_id: null,
user: null
... other fields as expected, like created and updated
}

The above object is what the last two logs print when trying to get the ticket. the connection isn't made in prismock, only when connecting to our database.

I did some digging around in the source code. When connecting, the user_id is passed as null to connectOrCreate in prismock.

I found a way to get the user_id on the item itself without having to directly pass it in the create object

for (const relation of field!.relationFromFields!) {
              accumulator[relation] = connect[relation]
            }

but even when that value is on item, the logic for finding the user didn't seem to be working. It seems to be hitting this block in create.ts

if (field!.relationFromFields!.length > 0) {
    const connected = findOne({ where: connect }, subDelegate, delegates);

connected is always null. Connect is passed to where as expected with the user_id. Further debugging showed that checking the User delegate (using .getItems()) returned no items, even though in my test code it returns the users I created. This is about as far as I got.

Relevant models:

model User {
  user_id            Int                   @id @default(autoincrement())
  tickets            Ticket[]
  tickets_gifted_by  Ticket[] @relation("gifted_by_user")
  tickets_gifted_to  Ticket[] @relation("gifted_to_user")
  ....
}

model Ticket {
  id Int @id @default(autoincrement())
  type TicketType
  gifted_to Int?
  gifted_to_user User? @relation(name: "gifted_to_user", fields: [gifted_to], references: [user_id])
  user_id Int
  user User @relation(fields: [user_id], references: [user_id])
  ...
}
@morintd
Copy link
Owner

morintd commented Jun 13, 2024

Hello, thanks a lot for the information, I'll have a look in the next few days!

@morintd
Copy link
Owner

morintd commented Jun 14, 2024

Hello @bogannathan I've not been able to reproduce this bug. Can you let me know if the test added in #957 correspond to your use-case?

Otherwise, would you be able to add one that reproduce your problem, which I can use to try and find what's going on?

@morintd morintd added invalid This doesn't seem right question Further information is requested labels Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants