Sequelize hasMany 关联不起作用
Sequelize hasMany association not working
我有一个产品型号:
module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define("product", {
SKU: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
categoryID: {
type: Sequelize.INTEGER,
}
});
Product.associate = function (models) {
Product.hasMany(models.Stock, { foreignKey: 'productID', as: 'stocks' })
};
return Product;
}
与 Stock
模型有 hasMany 关系:
module.exports = (sequelize, DataTypes) => {
const Stock = sequelize.define("stock", {
note: DataTypes.STRING,
})
Stock.associate = function (models) {
Stock.belongsTo(models.Product)
};
return Stock;
}
尝试查询产品时,出现以下错误:
exports.getAllAvailableProducts = (req, res) => {
const queryParams = req.query;
Product.findAll({
include: ['stocks'],
where: queryParams
}).then((products) => {
res.status(200).send(products);
})
};
(node:58108) UnhandledPromiseRejectionWarning: Error: Association with alias "stocks" does not exist on product
我是不是做错了什么?我也没有看到 FK 出现在我的数据库中
module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define("product", {
SKU: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
categoryID: {
type: Sequelize.INTEGER,
}
});
Product.associate = function (models) {
// models.stock is correct
Product.hasMany(models.stock, { foreignKey: 'productID', as: 'stocks' })
};
return Product;
}
module.exports = (sequelize, DataTypes) => {
const Stock = sequelize.define("stock", {
note: DataTypes.STRING,
})
Stock.associate = function (models) {
// models.product is correct
//Stock.belongsTo(models.product)
Stock.belongsTo(models.product, {
foreignKey: 'productId',
as: 'product'
})
};
return Stock;
}
models/index.js
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.user = require("./user.model.js")(sequelize, Sequelize);
db.category = require("./category.model.js")(sequelize, Sequelize);
db.product = require("./product.model.js")(sequelize, Sequelize);
db.orderline = require("./orderline.model.js")(sequelize, Sequelize);
db.order = require("./order.model.js")(sequelize, Sequelize);
db.stock = require("./stock.model.js")(sequelize, Sequelize);
// we need call associate function
db.stock.associate(db)
db.product.associate(db);
module.exports = db;
Product.findAll({
include: ['stocks'],
where: queryParams
}).then((products) => {
res.status(200).send(products);
})
我有一个产品型号:
module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define("product", {
SKU: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
categoryID: {
type: Sequelize.INTEGER,
}
});
Product.associate = function (models) {
Product.hasMany(models.Stock, { foreignKey: 'productID', as: 'stocks' })
};
return Product;
}
与 Stock
模型有 hasMany 关系:
module.exports = (sequelize, DataTypes) => {
const Stock = sequelize.define("stock", {
note: DataTypes.STRING,
})
Stock.associate = function (models) {
Stock.belongsTo(models.Product)
};
return Stock;
}
尝试查询产品时,出现以下错误:
exports.getAllAvailableProducts = (req, res) => {
const queryParams = req.query;
Product.findAll({
include: ['stocks'],
where: queryParams
}).then((products) => {
res.status(200).send(products);
})
};
(node:58108) UnhandledPromiseRejectionWarning: Error: Association with alias "stocks" does not exist on product
我是不是做错了什么?我也没有看到 FK 出现在我的数据库中
module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define("product", {
SKU: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
categoryID: {
type: Sequelize.INTEGER,
}
});
Product.associate = function (models) {
// models.stock is correct
Product.hasMany(models.stock, { foreignKey: 'productID', as: 'stocks' })
};
return Product;
}
module.exports = (sequelize, DataTypes) => {
const Stock = sequelize.define("stock", {
note: DataTypes.STRING,
})
Stock.associate = function (models) {
// models.product is correct
//Stock.belongsTo(models.product)
Stock.belongsTo(models.product, {
foreignKey: 'productId',
as: 'product'
})
};
return Stock;
}
models/index.js
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.user = require("./user.model.js")(sequelize, Sequelize);
db.category = require("./category.model.js")(sequelize, Sequelize);
db.product = require("./product.model.js")(sequelize, Sequelize);
db.orderline = require("./orderline.model.js")(sequelize, Sequelize);
db.order = require("./order.model.js")(sequelize, Sequelize);
db.stock = require("./stock.model.js")(sequelize, Sequelize);
// we need call associate function
db.stock.associate(db)
db.product.associate(db);
module.exports = db;
Product.findAll({
include: ['stocks'],
where: queryParams
}).then((products) => {
res.status(200).send(products);
})