编辑:
我取得了一些进展...我将关系更改为:
Survivor.associate = (models) => {
Survivor.belongsToMany(models.Survivor, { through: models.InfectedsReports, as: 'Reporter', foreignKey: 'ReportedId' })
Survivor.belongsToMany(models.Survivor, { through: models.InfectedsReports, as: 'Reported', foreignKey: 'ReporterId' })
}
现在它正确显示了两个字段,但是在插入中,这些值变为空!!!
原始问题:
我有以下表格:
幸存者:
module.exports = {
up: (queryInterface, DataTypes) => {
return queryInterface.createTable('Survivors', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
name: {
allowNull: false,
type: DataTypes.STRING,
},
gender: {
allowNull: false,
type: DataTypes.ENUM,
values: ['M', 'F']
},
isInfected: {
allowNull: false,
type: DataTypes.BOOLEAN,
defaultValue: 0
},
latitude: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null
},
longitude: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
})
},
down: (queryInterface) => {
return queryInterface.dropTable('Survivors');
}
};
其他幸存者报告的感染:
module.exports = {
up: (queryInterface, DataTypes) => {
return queryInterface.createTable('InfectedsReports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
ReporterId: {
allowNull: false,
type: DataTypes.INTEGER,
references: {
model: 'Survivors',
key: 'id',
}
},
ReportedId: {
allowNull: false,
type: DataTypes.INTEGER,
references: {
model: 'Survivors',
key: 'id',
}
},
});
},
down: (queryInterface) => {
return queryInterface.dropTable('SurvivorsItems');
}
};
模型:
module.exports = (sequelize, DataTypes) => {
const Survivor = sequelize.define('Survivor', {
name: DataTypes.STRING,
gender: {
type: DataTypes.ENUM,
values: ['M', 'F']
},
isInfected: DataTypes.BOOLEAN,
latitude: DataTypes.STRING,
longitude: DataTypes.STRING,
});
Survivor.associate = (models) => {
Survivor.belongsToMany(models.Survivor, { through: 'InfectedsReports', as: 'Reporter', foreignKey: 'id' })
Survivor.belongsToMany(models.Survivor, { through: 'InfectedsReports', as: 'Reported', foreignKey: 'id' })
}
return Survivor;
module.exports = (sequelize, DataTypes) => {
const InfectedsReports = sequelize.define('InfectedsReports',{
ReporterId: DataTypes.INTEGER,
ReportedId: DataTypes.INTEGER,
}, {
timestamps: false,
});
return InfectedsReports;
}
我插入失败,因为多对多自我关联:
执行(默认):插入
InfectedsReports
(id
)个值 (默认);未处理的拒绝SequelizeForeignKeyConstraintError: 无法添加或更新子行:外键约束失败 ({zombieresistance
。InfectedsReports
,约束InfectedsReports_ibfk_1
外键(ReporterId
)参考Survivors
(id
))
如果我删除一个belongsToMany声明,则会显示一个fild,而不显示另一个,并且插入中的id似乎为空:
控制器:
module.exports = {
async create(req, res) {
const { id, idReported } = req.params;
try {
const reporter = await Survivor.findByPk(id)
const reported = await Survivor.findByPk(idReported)
if (null === reported || null === reporter) {
res.status(404).send('Survivor not found')
}
const report = InfectedsReports.create({ idReporter: id, idReported: idReported })
res.send(report)
} catch (e) {
console.log(e)
res.status(500).send(e)
}
},
}
有人可以帮助我了解如何解决此问题吗?
tks ...
答案 0 :(得分:0)
已解决:
这种改变就是将死!
Survivor.associate = (models) => {
Survivor.belongsToMany(models.Survivor, { through: models.InfectedsReports, as: 'Reporter', foreignKey: 'ReportedId' })
Survivor.belongsToMany(models.Survivor, { through: models.InfectedsReports, as: 'Reported', foreignKey: 'ReporterId' })
}
谢谢!