mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:25:15 +00:00
fix(database): foreign key index in underscored table (#4473)
* fix: foreign key index in underscored table * chore: test
This commit is contained in:
parent
11343bc9be
commit
0dd3ea8d2e
@ -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();
|
||||
});
|
||||
});
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user