fix(db): filterByTk is ignored when both filter and filterByTk are used in o2m deletion (#6606)

* fix(db): `filterByTk` is ignored when both `filter` and `filterByTk` are used in o2m deletion

* test: add test
This commit is contained in:
YANG QIA 2025-04-01 23:28:41 +08:00 committed by GitHub
parent 33bfdf6cd7
commit 12e6824153
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 3 deletions

View File

@ -159,6 +159,7 @@ describe('has many repository', () => {
name: 'posts', name: 'posts',
fields: [ fields: [
{ type: 'string', name: 'title' }, { type: 'string', name: 'title' },
{ type: 'belongsTo', name: 'user' },
{ type: 'belongsToMany', name: 'tags', through: 'posts_tags' }, { type: 'belongsToMany', name: 'tags', through: 'posts_tags' },
{ type: 'hasMany', name: 'comments' }, { type: 'hasMany', name: 'comments' },
{ type: 'string', name: 'status' }, { type: 'string', name: 'status' },
@ -480,6 +481,51 @@ describe('has many repository', () => {
).not.toBeNull(); ).not.toBeNull();
}); });
test('destroy by pk and filter with association', async () => {
const u1 = await User.repository.create({
values: { name: 'u1' },
});
const UserPostRepository = new HasManyRepository(User, 'posts', u1.id);
const p1 = await UserPostRepository.create({
values: {
title: 't1',
status: 'published',
user: u1,
},
});
const p2 = await UserPostRepository.create({
values: {
title: 't2',
status: 'draft',
user: u1,
},
});
await UserPostRepository.destroy({
filterByTk: p1.id,
filter: {
user: {
id: u1.id,
},
},
});
expect(
await UserPostRepository.findOne({
filterByTk: p1.id,
}),
).toBeNull();
expect(
await UserPostRepository.findOne({
filterByTk: p2.id,
}),
).not.toBeNull();
});
test('destroy by pk', async () => { test('destroy by pk', async () => {
const u1 = await User.repository.create({ const u1 = await User.repository.create({
values: { name: 'u1' }, values: { name: 'u1' },

View File

@ -72,7 +72,13 @@ export class HasManyRepository extends MultipleRelationRepository {
const filterResult = this.parseFilter(options['filter'], options); const filterResult = this.parseFilter(options['filter'], options);
if (filterResult.include && filterResult.include.length > 0) { if (filterResult.include && filterResult.include.length > 0) {
return await this.destroyByFilter(options['filter'], transaction); return await this.destroyByFilter(
{
filter: options['filter'],
filterByTk: options['filterByTk'],
},
transaction,
);
} }
where.push(filterResult.where); where.push(filterResult.where);

View File

@ -179,9 +179,15 @@ export abstract class MultipleRelationRepository extends RelationRepository {
return false; return false;
} }
protected async destroyByFilter(filter: Filter, transaction?: Transaction) { protected async destroyByFilter(
options: {
filter?: Filter;
filterByTk?: TargetKey | TargetKey[];
},
transaction?: Transaction,
) {
const instances = await this.find({ const instances = await this.find({
filter: filter, ...options,
transaction, transaction,
}); });