首页 > 解决方案 > 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

标签: node.jssequelize.js

解决方案


推荐阅读