我正在尝试设计模型,以便将聚会表与与会者表和项目表关联起来。这样,我可以使用Party_Id查询两个表。
很明显,我没有按照正确的方式进行操作...是否有任何关于我在哪里出错的想法?
提前谢谢!
派对模型
module.exports = function(sequelize, DataTypes) {
var Party = sequelize.define("Party", {
eventName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
AuthenticationId: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
eventAddress: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
eventDate: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
eventTime: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
eventDescription: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
displayName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
}
});
Party.associate = function(models) {
Party.hasMany(models.Attendee, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
return Party;
};
与会者模型:
module.exports = function(sequelize, DataTypes) {
var Attendee = sequelize.define("Attendee", {
AuthenticationId: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
displayName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
}
});
Attendee.associate = function(models) {
Attendee.hasMany(models.Item, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
Attendee.associate = function(models) {
Attendee.belongsTo(models.Party, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
return Attendee;
};
项目模型
module.exports = function(sequelize, DataTypes) {
var Item = sequelize.define("Item", {
itemName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
qtyRequested: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
len: [1]
}
},
qtyCommited: {
type: DataTypes.INTEGER,
allowNull: true,
validate: {
len: [1]
}
},
hostAdded: {
type: DataTypes.BOOLEAN,
allowNull: true,
validate: {
len: [1]
}
},
AuthenticationId: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
},
displayName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [1]
}
}
});
Item.associate = function(models) {
Item.belongsTo(models.Attendee, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
return Item;
};
这是返回错误的查询:
app.get("/parties/:id", (req, res) => {
db.Party.findOne({
where: { id: req.params.id },
include: [
{
model: db.Attendee,
include: [
{
model: db.Item
}
]
}
]
})
.then(party => {
console.log(party)
})
.catch(function(error) {
console.log(error);
});
});
错误:
SequelizeEagerLoadingError: Item is not associated to Attendee!
答案 0 :(得分:0)
已解决的问题: 出于我要执行的操作,我必须按以下方式定义我的关联:
派对模式:
Party.associate = function(models) {
Party.hasMany(models.Attendee, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
与会者模型:
Attendee.associate = function(models) {
Attendee.hasMany(models.Item, {
foreignKey: "attendeeAuthenticationId",
allowNull: false,
onDelete: "cascade"
});
};
项目模型:
Item.associate = function(models) {
Item.belongsTo(models.Attendee, {
foreignKey: "attendeeAuthenticationId"
});
};
我的查询更改为:
db.Party.findOne({
where: { id: req.params.id },
include: [
{
model: db.Attendee,
include: [
{
model: db.Item
}
]
}
]
})
答案 1 :(得分:0)
真正的问题是这样:
Attendee.associate = function(models) {
Attendee.hasMany(models.Item, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
Attendee.associate = function(models) { // <---- This will override the upper one
Attendee.belongsTo(models.Party, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};
解决方案:
Attendee.associate = = function(models) {
Attendee.hasMany(models.Item, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
Attendee.belongsTo(models.Party, {
foreignKey: {
allowNull: false
},
onDelete: "cascade"
});
};