mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:47:20 +00:00
fix: eager load with long through table name
This commit is contained in:
parent
202ca9ec18
commit
7d1a087b50
@ -15,6 +15,49 @@ describe('Eager loading tree', () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('should handle eager loading with long field', async () => {
|
||||
const Through = db.collection({
|
||||
name: 'abc_abcd_abcd_abcdefg_abc_abc_a_abcdefghijk',
|
||||
});
|
||||
|
||||
const A = db.collection({
|
||||
name: 'a',
|
||||
fields: [
|
||||
{ type: 'string', name: 'name' },
|
||||
{
|
||||
type: 'belongsToMany',
|
||||
name: 'bs',
|
||||
target: 'b',
|
||||
through: 'abc_abcd_abcd_abcdefg_abc_abc_a_abcdefghijk',
|
||||
foreignKey: 'abc_abcd_abcdefg_abcd_abc',
|
||||
sourceKey: 'id',
|
||||
otherKey: 'b_id',
|
||||
targetKey: 'id',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const B = db.collection({
|
||||
name: 'b',
|
||||
fields: [{ type: 'string', name: 'name' }],
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
|
||||
await A.repository.create({
|
||||
values: {
|
||||
name: 'a1',
|
||||
bs: [{ name: 'b1' }, { name: 'b2' }],
|
||||
},
|
||||
});
|
||||
|
||||
const a = await A.repository.findOne({
|
||||
appends: ['bs'],
|
||||
});
|
||||
|
||||
expect(a.get('bs')).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should handle fields filter', async () => {
|
||||
const User = db.collection({
|
||||
name: 'users',
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Association, Includeable, Model, ModelStatic, Transaction } from 'sequelize';
|
||||
import { Association, HasOne, Includeable, Model, ModelStatic, Transaction } from 'sequelize';
|
||||
import lodash from 'lodash';
|
||||
|
||||
interface EagerLoadingNode {
|
||||
@ -157,12 +157,18 @@ export class EagerLoadingTree {
|
||||
if (associationType == 'BelongsToMany') {
|
||||
const foreignKeyValues = node.parent.instances.map((instance) => instance.get(association.sourceKey));
|
||||
|
||||
const pivotAssoc = new HasOne(association.target, association.through.model, {
|
||||
as: '_pivot',
|
||||
foreignKey: association.otherKey,
|
||||
sourceKey: association.targetKey,
|
||||
});
|
||||
|
||||
instances = await node.model.findAll({
|
||||
transaction,
|
||||
attributes: node.attributes,
|
||||
include: [
|
||||
{
|
||||
association: association.oneFromTarget,
|
||||
association: pivotAssoc,
|
||||
where: {
|
||||
[association.foreignKey]: foreignKeyValues,
|
||||
},
|
||||
@ -246,11 +252,9 @@ export class EagerLoadingTree {
|
||||
const sourceKey = association.sourceKey;
|
||||
const foreignKey = association.foreignKey;
|
||||
|
||||
const oneFromTarget = association.oneFromTarget;
|
||||
|
||||
for (const instance of node.instances) {
|
||||
const parentInstance = node.parent.instances.find(
|
||||
(parentInstance) => parentInstance.get(sourceKey) == instance[oneFromTarget.as].get(foreignKey),
|
||||
(parentInstance) => parentInstance.get(sourceKey) == instance['_pivot'].get(foreignKey),
|
||||
);
|
||||
|
||||
if (parentInstance) {
|
||||
|
Loading…
Reference in New Issue
Block a user