diff --git a/packages/core/database/src/interfaces/to-many-interface.ts b/packages/core/database/src/interfaces/to-many-interface.ts index 8b6aef65ad..06b7f8669c 100644 --- a/packages/core/database/src/interfaces/to-many-interface.ts +++ b/packages/core/database/src/interfaces/to-many-interface.ts @@ -19,7 +19,7 @@ export class ToManyInterface extends BaseInterface { const items = str.split(','); - const { filterKey, targetCollection, transaction } = ctx; + const { filterKey, targetCollection, transaction, field } = ctx; const targetInstances = await targetCollection.repository.find({ filter: { @@ -36,7 +36,19 @@ export class ToManyInterface extends BaseInterface { }); const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute; + const targetKey = field.options.targetKey; - return targetInstances.map((targetInstance) => targetInstance[primaryKeyAttribute]); + const values = targetInstances.map((targetInstance) => { + const result = { + [targetKey]: targetInstance[targetKey], + }; + + if (targetKey !== primaryKeyAttribute) { + result[primaryKeyAttribute] = targetInstance[primaryKeyAttribute]; + } + + return result; + }); + return values; } } diff --git a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts index dcf022b304..94365b9649 100644 --- a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts +++ b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts @@ -2067,4 +2067,84 @@ describe('xlsx importer', () => { expect(users[0].get('name')).toBe('User1'); expect(users[1].get('name')).toBe('User2'); }); + + it('should import with associations by target key', async () => { + const Post = app.db.collection({ + name: 'posts', + fields: [ + { + name: 'title', + type: 'string', + }, + { + name: 'tags', + type: 'belongsToMany', + through: 'post_tag', + targetKey: 'name', + interface: 'm2m', + }, + ], + }); + + const Tag = app.db.collection({ + name: 'tags', + fields: [ + { + name: 'name', + type: 'string', + unique: true, + }, + ], + }); + + await app.db.sync(); + + await Tag.repository.create({ + values: { + name: 't1', + }, + }); + + const templateCreator = new TemplateCreator({ + collection: Post, + columns: [ + { + dataIndex: ['title'], + defaultTitle: '标题', + }, + { + dataIndex: ['tags', 'name'], + defaultTitle: 'TagsName', + }, + ], + }); + + const template = (await templateCreator.run({ returnXLSXWorkbook: true })) as XLSX.WorkBook; + + const worksheet = template.Sheets[template.SheetNames[0]]; + + XLSX.utils.sheet_add_aoa(worksheet, [['Post1', 't1']], { + origin: 'A2', + }); + + const importer = new XlsxImporter({ + collectionManager: app.mainDataSource.collectionManager, + collection: Post, + columns: [ + { + dataIndex: ['title'], + defaultTitle: '标题', + }, + { + dataIndex: ['tags', 'name'], + defaultTitle: 'TagsName', + }, + ], + workbook: template, + }); + + await importer.run(); + + expect(await Post.repository.count()).toBe(1); + }); });