【问题标题】:Sequelize: <table> is not associated to <Table>Sequelize:<table> 未关联到 <Table>
【发布时间】:2020-06-17 21:14:14
【问题描述】:

我在绑定模型的关联时收到images is not associated to product! 错误。

ProductImagesProduct 关联,ProductImagesImages 模型关联。所以,我需要将images 属性分配给products 集合。

我尝试绑定的模型如下。

products.model.ts

const Product = SQLize.define('product', {
    id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }
    product_title: { type: new DataTypes.STRING(255) },
    vendor_id: { type: DataTypes.INTEGER }
}); 
Product.hasMany(ProductImages, {foreignKey: 'product_id', targetKey: 'id', as :'product_img_refs'})
export { Product };

product-images.model.ts

const ProductImages = SQLize.define('product_images', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  product_id: { type: DataTypes.INTEGER },
  product_image_id: { type: DataTypes.INTEGER }
  img_type_id: { type: DataTypes.INTEGER }
});
ProductImages.belongsTo(ImagesModel, {foreignKey: 'product_image_id', targetKey: 'id', as:'product_images' })
export {ProductImages}

images.model.ts:

const ImagesModel = SQLize.define('images', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  img_url: { type: DataTypes.STRING }
});
export { ImagesModel }

下面是我执行 SQLize 操作的存储库文件。

public async getProductData() {
  var prodData = Product.findAll({
    include: [
      { model: Vendor, as: 'vendor' }, 
      { model: ProductImages, as: 'product_img_refs' }
      { model: ImagesModel, as: 'product_images' }
    ]
  });
  return prodData;
}

=> 示例product_images 表记录。

=> 示例 images 表记录。

=> DB Schema 以获得更多可视化。

=> 我已经检查了这个answer,但它与我的模型无关,因为我有三个具有不同关联的模型。

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    不要同时使用 hasMany 和 belongsTo 关系,而是在 Product to Images 和从 Images to Product 上创建一个 many-to-many relationship

    您可以通过传递模型的名称来扩展自动生成的表(带有 ProductId 和 ImageId 列)。

    const ProductImages = SQLize.define('ProductImages', {
      // ...
    });
    Product.belongsToMany(ImagesModel, { through: ProductImages });
    ImagesModel.belongsToMany(Product, { through: ProductImages });
    

    您现在可以这样做:

    await Product.getImages();
    await Images.getProducts();
    

    或者在查询时使用include 选项。 There are examples in the documentation here。它会是这样的:

    await Product.findAll({
      include: ImagesModel,
    });
    // It will be nested as such:
    // { 
    //   fields from product
    //   Images: {
    //     fields from image
    //     ProductImages: {
    //       fields from the 'through' table
    //     }
    //   }
    // }
    

    【讨论】:

    • 您好,非常感谢您的详细解答。我已经尝试将belongsToMany 转换为ProductImages 模型,还更新了include 选项,但仍然出现相同的错误。你能告诉我include 选项到底应该是我的getProductData() 函数吗?
    • 所以,在将 belongsToMany 部分从 product-images 模型移动到 products 模型之后,错误已修复,但 { SqlError: (conn=13, no: 1054, SQLState: 42S22) Unknown column 'images-&gt;product_images.ProductId' in 'field list' } PS:忽略第一条评论。
    • Sequelize 默认使用 CamelCased 列名。您可以自定义它们以使用您正在使用的命名方案 foreignKeyotherKey 选项(就像在您的原始问题中一样)。数据库中 ProductImages 的表与 Sequelize 所期望的不同。要么放弃它,让 Sequelize 重新制作它,要么使用 foreignKey 匹配您的数据库架构。
    • 嗨,所以最后我得到了我所期望的结果......但缺少的一件事是,在应用group: ['product.id'] 之后,我只获得了product_image 表的第一条记录include 我在此屏幕截图中附加的选项:prnt.sc/t18c2m;那么我怎样才能获得完整的第 25 个 ID 到第 28 个 ID 记录,而不是只有第 25 个记录?我应该怎么做?
    • SQL 中的 group 命令就是这样做的:它将多条记录合并为一条。您可以使用 sequelize.fn 运行 GROUP_CONCAT (my)sql 函数来获取 id 的 CSV 列表...或者只是不将结果分组。
    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多