node.js - NodeJS Sequelize 多对多选择错误(我的错误是什么?)
问题描述
我有三张桌子:
1)品牌 2)类别 3) brand_categories 我正在尝试使用 sequelize 类别的品牌。
品牌型号
module.exports = (sequelize, DataTypes) => {
const Brands = sequelize.define('Brands', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
timestamps: false,
tableName: 'brands',
});
Brands.associate = function (models) {
Brands.belongsToMany(models.Categories, {
as: 'Categories',
through: models.BrandCategories,
foreignKey: 'brand_id'
});
};
return Brands;
};
类别模型
module.exports = (sequelize, DataTypes) => {
const Categories = sequelize.define('Categories', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
timestamps: false,
tableName: 'categories',
});
Categories.associate = function (models) {
Categories.belongsToMany(models.Brands, {
as: 'Brands',
through: models.BrandCategories,
foreignKey: 'category_id'
});
};
return Categories;
};
品牌类别模型
module.exports = (sequelize, DataTypes) => {
const BrandCategories = sequelize.define('BrandCategories', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
}
}, {
timestamps: false,
tableName: 'brand_categories'
});
BrandCategories.associate = function (models) {
};
return BrandCategories;
};
我正在尝试使用这样的控制器
brands = await model.findAll({
include: [{
model: models.Categories,
as: 'Categories'
}]
});
它返回我错误
"name": "SequelizeDatabaseError",
"parent": {
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'Categories->BrandCategories.id' in 'field list'",
"sql": "SELECT `Brands`.`id`, `Brands`.`name`, `Brands`.`slug`, `Brands`.`description_uz`, `Brands`.`description_ru`, `Brands`.`description_en`, `Brands`.`logo`, `Brands`.`status`, `Categories`.`id` AS `Categories.id`, `Categories`.`slug` AS `Categories.slug`, `Categories`.`name_uz` AS `Categories.name_uz`, `Categories`.`name_ru` AS `Categories.name_ru`, `Categories`.`name_en` AS `Categories.name_en`, `Categories`.`image` AS `Categories.image`, `Categories`.`parent_id` AS `Categories.parent_id`, `Categories`.`adv_image` AS `Categories.adv_image`, `Categories`.`adv_url` AS `Categories.adv_url`, `Categories`.`status` AS `Categories.status`, `Categories`.`meta_keys` AS `Categories.meta_keys`, `Categories`.`meta_descriptions` AS `Categories.meta_descriptions`, `Categories`.`createdAt` AS `Categories.createdAt`, `Categories`.`updatedAt` AS `Categories.updatedAt`, `Categories->BrandCategories`.`id` AS `Categories.BrandCategories.id`, `Categories->BrandCategories`.`brand_id` AS `Categories.BrandCategories.brand_id`, `Categories->BrandCategories`.`category_id` AS `Categories.BrandCategories.category_id` FROM `brands` AS `Brands` LEFT OUTER JOIN ( `brand_categories` AS `Categories->BrandCategories` INNER JOIN `categories` AS `Categories` ON `Categories`.`id` = `Categories->BrandCategories`.`category_id`) ON `Brands`.`id` = `Categories->BrandCategories`.`brand_id`;"
},
我不明白为什么要添加 -> 这个符号?我的错误是什么?我阅读了所有文件,但找不到。请帮我。 数据库:SQL
解决方案
推荐阅读
- java - 如何在java中使用特定语言键盘
- go - 将数组传递给 go-pg 查询
- java - 我的程序不会通过 HttpPost
- postgresql - 从不同的表中添加 COUNT(*),包括 WHERE 语句和 GROUP BY
- xml - XSLT:来自多个元素的排序列表
- javascript - 如何使这个更紧凑?悬停时的 JQuery 图像幻灯片(视频缩略图预览)
- c# - iTextSharp 4.1.6 显示来自字节 [] 的图像
- python - Python Zeep - Any 的缺失元素
- kendo-ui - 代码背后的 Kendo UI 图表
- node.js - 未处理的拒绝 MongoError:必须指定端口