fix(database): refresh indexes (#1127)

This commit is contained in:
chenos 2022-11-22 20:31:29 +08:00 committed by GitHub
parent 3556ddc730
commit 7bffc94b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ import {
QueryInterfaceDropTableOptions, QueryInterfaceDropTableOptions,
SyncOptions, SyncOptions,
Transactionable, Transactionable,
Utils, Utils
} from 'sequelize'; } from 'sequelize';
import { Database } from './database'; import { Database } from './database';
import { Field, FieldOptions } from './fields'; import { Field, FieldOptions } from './fields';
@ -379,6 +379,7 @@ export class Collection<
} }
return item; return item;
}); });
this.refreshIndexes();
} }
removeIndex(fields: any) { removeIndex(fields: any) {
@ -391,6 +392,21 @@ 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();
}
refreshIndexes() {
// @ts-ignore
const indexes: any[] = this.model._indexes;
// @ts-ignore
this.model._indexes = indexes.filter((item) => {
for (const field of item.fields) {
if (!this.model.rawAttributes[field]) {
return false;
}
}
return true;
});
} }
async sync(syncOptions?: SyncOptions) { async sync(syncOptions?: SyncOptions) {