Javascript “续集”;包括「;不在多对多关系中工作
我是新的节点和续集,我正在建立一个基本的电子商务。 这些是我的型号:Javascript “续集”;包括「;不在多对多关系中工作,javascript,node.js,sequelize.js,many-to-many,associations,Javascript,Node.js,Sequelize.js,Many To Many,Associations,我是新的节点和续集,我正在建立一个基本的电子商务。 这些是我的型号: const Product = sequelize.define('product', { id: { type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true, }, name: { type: DataTypes.STRING, allowNull: false
const Product = sequelize.define('product', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
});
const Sale = sequelize.define('sale', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
isDelivered: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
});
Sale.belongsToMany(Product, { through: SaleItem });
Product.belongsToMany(Sale, { through: SaleItem });
这些关系是:
const Product = sequelize.define('product', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
});
const Sale = sequelize.define('sale', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
isDelivered: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
});
Sale.belongsToMany(Product, { through: SaleItem });
Product.belongsToMany(Sale, { through: SaleItem });
当我尝试获取“SaleItems”时,问题就开始了
我得到的答复是:
SequelizeAgerLoadingError:产品未与saleItem关联
这很奇怪,因为我在一对多关系上也做了同样的事情,而且工作得很完美
感谢您的时间:)您在
产品
和销售
之间有关联,但在它们和销售项目
之间没有关联
您现有的关联只允许执行如下查询:
Sale.findAll({
include: [Product],
})
.then(response => {
console.log(response);
})
Product.findAll({
include: [Sale],
})
.then(response => {
console.log(response);
})
SaleItem.belongsTo(Product);
SaleItem.belongsTo(Sale);
您需要为SaleItem
添加如下关联:
Sale.findAll({
include: [Product],
})
.then(response => {
console.log(response);
})
Product.findAll({
include: [Sale],
})
.then(response => {
console.log(response);
})
SaleItem.belongsTo(Product);
SaleItem.belongsTo(Sale);
为了获得
SaleItem
记录以及相关的模型实例。我发现了类似的问题,也许可以帮助您。祝你好运