我的目标是能够通过品牌名称和型号名称找到所有产品。但是,Sequelize 仅返回许多其他类似记录中的一个记录。如果它确实返回了多个记录,则与找到的第一条记录具有相同属性的其他记录将是 null
。例如,数组中的第一条记录将具有属性 name: iPhone
,具有完全相同属性的第二条记录将显示为 name: null
,而它应该是 name: iPhone
。
在我的数据库中,我有以下表格:Products
、 Brands
、 Models
和 Suppliers
。 Products
表包含 brand_id
、 model_id
等外键。 Brands
、 Models
和 Suppliers
具有属性: id
。
我已将关系设置如下:
Products.hasOne(Brands, { foreignKey: 'id' });
Products.hasOne(Models, { foreignKey: 'id' });
Products.hasOne(Suppliers, { foreignKey: 'id' });
Brands.belongsTo(Products);
Models.belongsTo(Products);
Suppliers.belongsTo(Products);
在我的搜索功能中,我尝试按品牌和型号名称查找与我的查询匹配的所有产品。
const getSearch = (req, res) => {
const { query: { query } } = req;
Products.findAll({
where: Sequelize.where(Sequelize.fn('concat', Sequelize.col('Brand.name'), ' ', Sequelize.col('Model.name')), {
[Op.substring]: query
}),
include: [
{ model: Brands, attributes: ['name'] },
{ model: Models, attributes: ['name'] },
{ model: Suppliers, attributes: ['name'] },
],
attributes: ['id', 'price']
})
.then((data) => {
console.log(data);
res.send(data);
})
.catch((err) => (console.log(err)));
};
在我的数据库中,我有两个具有完全相同数据但不同 id 的产品行。调用
getSearch
时,我希望在数组中看到两个对象,因为它们具有相同的品牌名称和型号名称。相反,我看到了一个。这是我的模型的样子:
产品
class Products extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
url_id: {
type: DataTypes.INTEGER,
allowNull: true
},
brand_id: {
type: DataTypes.INTEGER,
allowNull: true
},
model_id: {
type: DataTypes.INTEGER,
allowNull: true
},
supplier_id: {
type: DataTypes.INTEGER,
allowNull: true
},
image: {
type: DataTypes.STRING,
allowNull: true
},
description: {
type: DataTypes.STRING,
allowNull: true
},
price: {
type: DataTypes.DOUBLE,
allowNull: true
}
},
{
modelName: 'Products',
timestamps: false,
sequelize
}
);
}
}
楷模
class Models extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
},
colour_id: {
type: DataTypes.INTEGER,
allowNull: true
},
storage_capacity_id: {
type: DataTypes.INTEGER,
allowNull: true
}
},
{
modelName: 'Models',
timestamps: false,
sequelize
}
);
}
}
品牌
class Brands extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
}
},
{
modelName: 'Brands',
timestamps: false,
sequelize
}
);
}
}
供应商
class Suppliers extends Model {
static init(sequelize, DataTypes) {
return super.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: true
}
},
{
modelName: 'Suppliers',
timestamps: false,
sequelize
}
);
}
}
我在这里做错了什么?
最佳答案
您有关联错误。只需将 hasOne 更改为 hasMany 即可。
关于javascript - 为什么 Sequelize 只使用 findAll() 返回一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57749107/