fix: eager load with long through table name

This commit is contained in:
chareice 2023-05-27 17:35:30 +08:00
parent 202ca9ec18
commit 7d1a087b50
2 changed files with 52 additions and 5 deletions

View File

@ -15,6 +15,49 @@ describe('Eager loading tree', () => {
await db.close(); 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 () => { it('should handle fields filter', async () => {
const User = db.collection({ const User = db.collection({
name: 'users', name: 'users',

View File

@ -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'; import lodash from 'lodash';
interface EagerLoadingNode { interface EagerLoadingNode {
@ -157,12 +157,18 @@ export class EagerLoadingTree {
if (associationType == 'BelongsToMany') { if (associationType == 'BelongsToMany') {
const foreignKeyValues = node.parent.instances.map((instance) => instance.get(association.sourceKey)); 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({ instances = await node.model.findAll({
transaction, transaction,
attributes: node.attributes, attributes: node.attributes,
include: [ include: [
{ {
association: association.oneFromTarget, association: pivotAssoc,
where: { where: {
[association.foreignKey]: foreignKeyValues, [association.foreignKey]: foreignKeyValues,
}, },
@ -246,11 +252,9 @@ export class EagerLoadingTree {
const sourceKey = association.sourceKey; const sourceKey = association.sourceKey;
const foreignKey = association.foreignKey; const foreignKey = association.foreignKey;
const oneFromTarget = association.oneFromTarget;
for (const instance of node.instances) { for (const instance of node.instances) {
const parentInstance = node.parent.instances.find( 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) { if (parentInstance) {