mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-04 21:19:27 +08:00
fix: many-to-many relationship fails to query when using non-primary key as foreign key
This commit is contained in:
parent
fd8606e69e
commit
1981b2c05f
@ -963,3 +963,47 @@ describe('belongs to many', () => {
|
||||
await transaction.commit();
|
||||
});
|
||||
});
|
||||
|
||||
describe('belongs to many', () => {
|
||||
let db: Database;
|
||||
let A;
|
||||
let B;
|
||||
|
||||
beforeEach(async () => {
|
||||
db = await createMockDatabase();
|
||||
await db.clean({ drop: true });
|
||||
A = db.collection({
|
||||
name: 'a',
|
||||
fields: [
|
||||
{ type: 'string', name: 'code', unique: true },
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
target: 'b',
|
||||
targetKey: 'code',
|
||||
sourceKey: 'code',
|
||||
foreignKey: 'a_code',
|
||||
otherKey: 'b_code',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
B = db.collection({
|
||||
name: 'b',
|
||||
fields: [{ type: 'string', name: 'code', unique: true }],
|
||||
});
|
||||
|
||||
await db.sync({ force: true });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('should get database instance in repository', async () => {
|
||||
const a = await A.create({
|
||||
values: {
|
||||
b: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -8,12 +8,12 @@
|
||||
*/
|
||||
|
||||
import lodash from 'lodash';
|
||||
import qs from 'qs';
|
||||
import { FindAttributeOptions, ModelStatic, Op, Sequelize } from 'sequelize';
|
||||
import { Collection } from './collection';
|
||||
import { Database } from './database';
|
||||
import FilterParser from './filter-parser';
|
||||
import { Appends, Except, FindOptions } from './repository';
|
||||
import qs from 'qs';
|
||||
|
||||
const debug = require('debug')('noco-database');
|
||||
|
||||
@ -43,7 +43,10 @@ export class OptionsParser {
|
||||
ctx: options?.context,
|
||||
},
|
||||
});
|
||||
this.context = context;
|
||||
this.context = {
|
||||
targetKey: options['__targetKey'],
|
||||
...context,
|
||||
};
|
||||
}
|
||||
|
||||
static appendInheritInspectAttribute(include, collection): any {
|
||||
|
@ -12,6 +12,7 @@ import { HasOne, MultiAssociationAccessors, Sequelize, Transaction } from 'seque
|
||||
import injectTargetCollection from '../decorators/target-collection-decorator';
|
||||
import { updateModelByValues } from '../update-associations';
|
||||
import { UpdateGuard } from '../update-guard';
|
||||
import { valuesToFilter } from '../utils/filter-utils';
|
||||
import { RelationRepository, transaction } from './relation-repository';
|
||||
import {
|
||||
AssociatedOptions,
|
||||
@ -19,11 +20,10 @@ import {
|
||||
DestroyOptions,
|
||||
Filter,
|
||||
FindOptions,
|
||||
FirstOrCreateOptions,
|
||||
TargetKey,
|
||||
UpdateOptions,
|
||||
FirstOrCreateOptions,
|
||||
} from './types';
|
||||
import { valuesToFilter } from '../utils/filter-utils';
|
||||
|
||||
export abstract class MultipleRelationRepository extends RelationRepository {
|
||||
async targetRepositoryFilterOptionsBySourceValue(): Promise<any> {
|
||||
@ -63,6 +63,8 @@ export abstract class MultipleRelationRepository extends RelationRepository {
|
||||
|
||||
return targetRepository.find({
|
||||
include: [appendFilter],
|
||||
// @ts-ignore
|
||||
__targetKey: association.targetKey,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@ -118,6 +120,7 @@ export abstract class MultipleRelationRepository extends RelationRepository {
|
||||
async findOne(options?: FindOptions): Promise<any> {
|
||||
const transaction = await this.getTransaction(options, false);
|
||||
const rows = await this.find({ ...options, limit: 1, transaction });
|
||||
console.log('rows', rows);
|
||||
return rows.length == 1 ? rows[0] : null;
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ import {
|
||||
WhereOperators,
|
||||
} from 'sequelize';
|
||||
|
||||
import { BelongsToArrayRepository } from './belongs-to-array/belongs-to-array-repository';
|
||||
import { Collection } from './collection';
|
||||
import { Database } from './database';
|
||||
import mustHaveFilter from './decorators/must-have-filter-decorator';
|
||||
@ -38,7 +39,6 @@ import FilterParser from './filter-parser';
|
||||
import { Model } from './model';
|
||||
import operators from './operators';
|
||||
import { OptionsParser } from './options-parser';
|
||||
import { BelongsToArrayRepository } from './belongs-to-array/belongs-to-array-repository';
|
||||
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
||||
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
||||
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
||||
|
Loading…
x
Reference in New Issue
Block a user