Skip to content

Commit

Permalink
fix(db-postgres): cascade delete FKs on hasMany relationships (#6735)
Browse files Browse the repository at this point in the history
## Description

This PR fixes #6485.

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
  • Loading branch information
ikenox authored Jun 19, 2024
1 parent 3364385 commit 9ecc6c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/db-postgres/src/schema/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export const traverseFields = ({
name: `${selectTableName}_parent_fk`,
columns: [cols.parent],
foreignColumns: [adapter.tables[parentTableName].id],
}),
}).onDelete('cascade'),
parentIdx: (cols) => index(`${selectTableName}_parent_idx`).on(cols.parent),
}

Expand Down
21 changes: 21 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
tabsFieldsSlug,
textFieldsSlug,
} from './slugs'
import { NotFound } from '../../packages/payload/src/errors'

let client: RESTClient
let graphQLClient: GraphQLClient
Expand Down Expand Up @@ -351,6 +352,26 @@ describe('Fields', () => {
expect(updatedDoc.selectHasMany).toEqual(['one', 'two'])
})

// https://github.com/payloadcms/payload/issues/6485
it('delete with selectHasMany relationship', async () => {
const { id } = await payload.create({
collection: 'select-fields',
data: {
selectHasMany: ['one', 'two'],
},
})
await payload.delete({
collection: 'select-fields',
id,
})
await expect(
payload.findByID({
collection: 'select-fields',
id,
}),
).rejects.toThrow(NotFound)
})

it('should query hasMany in', async () => {
const hit = await payload.create({
collection: 'select-fields',
Expand Down

0 comments on commit 9ecc6c8

Please sign in to comment.