我正在将sequelize与nodejs一起使用,并且在关系方面遇到困难。
我有3张桌子(地方,设备,棚子,区域),在一个地方我有1台设备,它在棚子里并属于一个区域。
模型如下:
本地:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('local', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
required: true,
unique: true
},
name: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idEquipment: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idShed: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
situation: {
type: dataTypes.BOOLEAN,
allowNull: false,
required: true,
defaultValue: true
},
capacity: {
type: dataTypes.FLOAT,
allowNull: false,
required: true
},
posX: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posY: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posZ: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
model.belongsTo(models.equipment, {
foreignKey: 'idEquipment'
});
// model.belongsTo(models.shed, {
// foreignKey: 'idShed'
// });
};
return model;
};[]
设备:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('equipment', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idHangar: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
dateControlPlan: {
type: dataTypes.DATE,
allowNull: true,
required: false
},
idUserControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.hasOne(models.local, {
foreignKey: 'idEquipment'
});
};
return model;
};
棚屋:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('shed', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
// model.hasOne(models.local, {
// foreignKey: 'idShed'
// });
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
};
return model;
};
区域:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('area', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING,
unique: true,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsToMany(models.company, {
through: 'companyArea',
foreignKeyConstraint: true,
foreignKey: 'idArea'
});
model.hasOne(models.shed, {
foreignKey: 'idArea'
});
model.hasOne(models.local, {
foreignKey: 'idArea'
});
};
return model;
};
当我添加棚子关系时,它告诉我没有关系,通常在拍摄所有东西时会
[SQL错误] SequelizeDatabaseError:关系“ public.sheds”不存在退出 [SQL错误]关系“ public.sheds”不存在退出
我正在使用postgres数据库。
那是哪里的错误,那是写作的错误吗?还是模板不能同时具有belongsTo和hasOne关系?