How does sequelize establish multi-table associations and query?

problem description:
there are three tables

Table relationship description:
the 1.category table holds the directory id and the name. The
2.category_product table stores the id,product_id of the category table corresponding to category_id and the corresponding product table, which contains the details of the product.

< hr >

current modeling relationship:
in category model

category.hasMany(category_product,{foreignKey:"id",targetKey:"category_id"})

in category_product model

category_product.belongsTo(Product, {foreignKey: "product_id",targetKey: "id"})
< hr >

error problem:

SequelizeDatabaseError: Unknown column "category_product.id" in "field list"

1. Check to see if your model definition fields and types are correct.
2. Take a closer look at these methods and see if the call is correct and then debug to see the result.

hasOne - 
belongsTo - 
hasMany - 
belongsToMany - N:MsourceIdtargetId

Chinese documents are as follows: https://itbilu.com/nodejs/npm.


//category_productcategory
category.hasMany(category_product,{foreignKey:'category_id'})
category_product.belongsTo(category,{foreignKey:'category_id'})

//category_productProduct
Product.hasMany(category_product,{foreignKey:'product_id'})
category_product.belongsTo(Product, {foreignKey: 'product_id'})

in addition, hasMany has abolished targetKey . If you want to use a non- id as the association key, use sourceKey , but not in terms of your data structure.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c55c-40891.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c55c-40891.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?