node.js - Sequelize : join query adding extra column which is not in model

标签 node.js sequelize.js

我正在将连接查询应用于两个模型的 Node 的 Sequelize 包。但它加入了 vendor 模型的列,这不是模型属性的一部分。

产品型号::

import { Vendor } from '../vendor/vendor.model'

const Product = SQLize.define('product', {
    id: {
        type: DataTypes.INTEGER.UNSIGNED,
        autoIncrement: true,
        primaryKey: true,
    }
    product_title: {
        type: new DataTypes.STRING(255),
        allowNull: false
    },
    vendor_id: {
        type: DataTypes.INTEGER.UNSIGNED
    },
    createdAt: {
        type: 'TIMESTAMP',
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
        allowNull: false
    },
    updatedAt: {
        type: 'TIMESTAMP',
        allowNull: true
    }
});

Product.hasOne(Vendor, {foreignKey: 'id'})
Vendor.belongsTo(Product)

export { Product };

供应商型号::
const Vendor = SQLize.define('vendor', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
        autoIncrement: true,
        primaryKey: true,
  },
  first_name: {
    type: new DataTypes.STRING(100),
  },
  last_name: {
    type: new DataTypes.STRING(100),
  }
});

export { Vendor }

这是我在其上执行 sql 操作的存储库文件::
public async getProductData() {

    var prodData = Product.findAll({
      subQuery: false,
      include: {
        model: Vendor
      }
    });

    console.log(prodData); 
    return prodData;
  }

但它一直在抛出错误:

SqlError: (conn=20, no: 1054, SQLState: 42S22) 'field list' 中的未知列 'vendor.ProductId'

附:
DB Structure
Sequelize 版本:5.21.12

最佳答案

您定义了关联定义,例如供应商具有指向产品的链接,但根据您的模型定义,产品具有指向供应商的链接。

您还忘记在第二个关联定义中指明 foreignKey 选项:

Product.belongsTo(Vendor, {foreignKey: 'id'})
Vendor.hasOne(Product, {foreignKey: 'id'})

关于node.js - Sequelize : join query adding extra column which is not in model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62388837/

相关文章:

javascript - Socket.io 在第二个窗口上重复发出

javascript - 使用 Node 提供 Vue 应用程序时,构建会导致空 HTML 页面出现语法错误

javascript - 当我过滤属性时,Sequelize 对结果进行分组

javascript - 发出 Axios GET 请求时无限循环 - 循环不会关闭

node.js - 有没有办法在 Sequelize.js 中包含包含在 JSONB 中的属性?

node.js - (更新解决方案)Express Web 应用程序服务抛出 "SyntaxError: Unexpected token >"

node.js - 使用 OR 条件序列化 FindOrCreate 函数

sql - 在 LIKE 语句中对多个值进行序列化

express - 如何通过选项来 Sequelize afterBulkUpdate

javascript - 如何在Sequelize中按多对多关系排序?