我正在将连接查询应用于两个模型的 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/