首页 > 解决方案 > 为什么 Sequelize 使用 findAll() 只返回一个对象?

问题描述

我的目标是能够通过品牌名称和型号名称找到所有产品。然而,Sequelize 只返回了许多其他类似记录中的一个记录。如果它确实返回了多个记录,则与找到的第一个记录具有相同属性的其他记录将是null. 例如,数组中的第一条记录将具有属性name: iPhone,具有完全相同属性的第二条记录将显示为name: nullwhen it should be name: iPhone

在我的数据库中,我有以下表格:

Products, Brands,ModelsSuppliers. 该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
      }
    );
  }
}

我在这里做错了什么?

标签: javascriptnode.jsexpresssequelize.js

解决方案


您在关联中有错误。只需将 hasOne 更改为 hasMany 即可。


推荐阅读