mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 11:56:29 +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)) {
|
if (lodash.isEqual(item.fields, indexName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const name: string = item.fields.join(',');
|
const name: string = item.fields.join(',');
|
||||||
if (name.startsWith(`${indexName.join(',')},`)) {
|
if (name.startsWith(`${indexName.join(',')},`)) {
|
||||||
return;
|
return;
|
||||||
@ -701,6 +702,7 @@ export class Collection<
|
|||||||
this.model._indexes = indexes.filter((item) => {
|
this.model._indexes = indexes.filter((item) => {
|
||||||
return !lodash.isEqual(item.fields, fields);
|
return !lodash.isEqual(item.fields, fields);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.refreshIndexes();
|
this.refreshIndexes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -715,14 +717,10 @@ export class Collection<
|
|||||||
this.model._indexes = lodash.uniqBy(
|
this.model._indexes = lodash.uniqBy(
|
||||||
indexes
|
indexes
|
||||||
.filter((item) => {
|
.filter((item) => {
|
||||||
return item.fields.every((field) =>
|
return item.fields.every((field) => this.model.rawAttributes[field]);
|
||||||
Object.values(this.model.rawAttributes).find((fieldVal) => fieldVal.field === field),
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
if (this.options.underscored) {
|
item.fields = item.fields.map((field) => this.model.rawAttributes[field].field);
|
||||||
item.fields = item.fields.map((field) => snakeCase(field));
|
|
||||||
}
|
|
||||||
return item;
|
return item;
|
||||||
}),
|
}),
|
||||||
'name',
|
'name',
|
||||||
|
Loading…
Reference in New Issue
Block a user