From 0dd3ea8d2eba4d53a3de6f397fe2caa91c485ad2 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 23 May 2024 19:49:49 +0800 Subject: [PATCH] fix(database): foreign key index in underscored table (#4473) * fix: foreign key index in underscored table * chore: test --- .../src/__tests__/sync/foreign-key.test.ts | 61 +++++++++++++++++++ packages/core/database/src/collection.ts | 10 ++- 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 packages/core/database/src/__tests__/sync/foreign-key.test.ts diff --git a/packages/core/database/src/__tests__/sync/foreign-key.test.ts b/packages/core/database/src/__tests__/sync/foreign-key.test.ts new file mode 100644 index 0000000000..730a5d58e7 --- /dev/null +++ b/packages/core/database/src/__tests__/sync/foreign-key.test.ts @@ -0,0 +1,61 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { primaryKey } from '@nocobase/client'; +import { Database, mockDatabase } from '@nocobase/database'; + +describe('foreign key', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase({}); + + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + it('should create index for foreign key', async () => { + const users = db.collection({ + name: 'users', + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }); + + const posts = db.collection({ + name: 'posts', + fields: [ + { + type: 'string', + name: 'title', + }, + { + type: 'belongsTo', + name: 'user', + target: 'users', + }, + ], + }); + + await db.sync(); + + const foreignKey = posts.model.rawAttributes['userId'].field; + + const indexes = await db.sequelize.getQueryInterface().showIndex(posts.getTableNameWithSchema()); + + // @ts-ignore + expect(indexes.some((index) => index.fields.some((field) => field.attribute === foreignKey))).toBeTruthy(); + }); +}); diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index da815f228a..ee72e126dd 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -664,6 +664,7 @@ export class Collection< if (lodash.isEqual(item.fields, indexName)) { return; } + const name: string = item.fields.join(','); if (name.startsWith(`${indexName.join(',')},`)) { return; @@ -701,6 +702,7 @@ export class Collection< this.model._indexes = indexes.filter((item) => { return !lodash.isEqual(item.fields, fields); }); + this.refreshIndexes(); } @@ -715,14 +717,10 @@ export class Collection< this.model._indexes = lodash.uniqBy( indexes .filter((item) => { - return item.fields.every((field) => - Object.values(this.model.rawAttributes).find((fieldVal) => fieldVal.field === field), - ); + return item.fields.every((field) => this.model.rawAttributes[field]); }) .map((item) => { - if (this.options.underscored) { - item.fields = item.fields.map((field) => snakeCase(field)); - } + item.fields = item.fields.map((field) => this.model.rawAttributes[field].field); return item; }), 'name',