#Deleting a linked/related collection item in code doesn’t cascade

7 messages · Page 1 of 1 (latest)

barren turtle
#
const now = new Date().toISOString();
const event = await payload.create({
    collection: "event",
    data: {
        id: `test-event-categorisation-19-${now}`,
        title: "Event that is revolving around the topic democracy",
        dateTime: now,
        sourceCalendar,
        // the `sector` key is set in the events beforeChange hook
    },
});
expect(event).toBeDefined();
expect((event.sector as Sector).title).toEqual(sector.title);

await payload.delete({
    collection: "sector",
    id: sector.id,
});
expect(event.sector).toBeNull();

I get this test failure:

error: expect(received).toBeNull()

Received: {
  id: 1,
  title: "Test Sector",
  wordList: [
    {
      id: "69a196bd0a71d4029d16709b",
      word: "Test",
    }, {
      id: "69a196bd0a71d4029d16709c",
      word: "democracy",
    }
  ],
  updatedAt: "2026-02-27T13:06:05.663Z",
  createdAt: "2026-02-27T13:06:05.662Z",
}

Do I need to await database changes or anything for this test to pass or what am I doing wrong? When I assign and delete a sector to an event in the ui, the change propagates corrently

frigid slateBOT
sturdy quail
#

Try passing a req to your Payload oeprations

#
const myTestReq = { payload }
await payload.delete({ ..., req: myTestReq })
barren turtle
#

I just never re-fetched the event and checked against the initial const …

#

Thanks though!

sturdy quail
#

No worries, glad you got it sorted out!