fix(database): cannot read properties of undefined (reading 'findField')

This commit is contained in:
chenos 2022-11-24 15:36:51 +08:00
parent a8cabbab56
commit 71120ab3c4
2 changed files with 16 additions and 13 deletions

View File

@ -1,8 +1,8 @@
import { omit } from 'lodash';
import { BelongsToOptions as SequelizeBelongsToOptions, Utils } from 'sequelize';
import { Reference } from '../features/ReferencesMap';
import { checkIdentifier } from '../utils';
import { BaseRelationFieldOptions, RelationField } from './relation-field';
import { Reference } from '../features/ReferencesMap';
export class BelongsToField extends RelationField {
static type = 'belongsTo';
@ -84,9 +84,9 @@ export class BelongsToField extends RelationField {
const foreignKey = this.options.foreignKey;
const field1 = collection.getField(foreignKey);
const field2 = tcoll.findField((field) => {
const field2 = tcoll ? tcoll.findField((field) => {
return field.type === 'hasMany' && field.foreignKey === foreignKey;
});
}) : null;
if (!field1 && !field2) {
collection.model.removeAttribute(foreignKey);

View File

@ -5,12 +5,12 @@ import {
ForeignKeyOptions,
HasManyOptions,
HasManyOptions as SequelizeHasManyOptions,
Utils,
Utils
} from 'sequelize';
import { Collection } from '../collection';
import { Reference } from '../features/ReferencesMap';
import { checkIdentifier } from '../utils';
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
import { Reference } from '../features/ReferencesMap';
export interface HasManyFieldOptions extends HasManyOptions {
/**
@ -155,15 +155,18 @@ export class HasManyField extends RelationField {
database.removePendingField(this);
// 如果关系表内没有显式的创建外键字段,删除关系时,外键也删除掉
const tcoll = database.getCollection(this.target);
const foreignKey = this.options.foreignKey;
const field = tcoll.findField((field) => {
if (field.name === foreignKey) {
return true;
if (tcoll) {
const foreignKey = this.options.foreignKey;
const field = tcoll.findField((field) => {
if (field.name === foreignKey) {
return true;
}
return field.type === 'belongsTo' && field.foreignKey === foreignKey;
});
if (!field) {
tcoll.model.removeAttribute(foreignKey);
}
return field.type === 'belongsTo' && field.foreignKey === foreignKey;
});
if (!field) {
tcoll.model.removeAttribute(foreignKey);
}
const association = collection.model.associations[this.name];