mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
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:
parent
33bfdf6cd7
commit
12e6824153
@ -159,6 +159,7 @@ describe('has many repository', () => {
|
||||
name: 'posts',
|
||||
fields: [
|
||||
{ type: 'string', name: 'title' },
|
||||
{ type: 'belongsTo', name: 'user' },
|
||||
{ type: 'belongsToMany', name: 'tags', through: 'posts_tags' },
|
||||
{ type: 'hasMany', name: 'comments' },
|
||||
{ type: 'string', name: 'status' },
|
||||
@ -480,6 +481,51 @@ describe('has many repository', () => {
|
||||
).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 () => {
|
||||
const u1 = await User.repository.create({
|
||||
values: { name: 'u1' },
|
||||
|
@ -72,7 +72,13 @@ export class HasManyRepository extends MultipleRelationRepository {
|
||||
const filterResult = this.parseFilter(options['filter'], options);
|
||||
|
||||
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);
|
||||
|
@ -179,9 +179,15 @@ export abstract class MultipleRelationRepository extends RelationRepository {
|
||||
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({
|
||||
filter: filter,
|
||||
...options,
|
||||
transaction,
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user