更改关联后在 Eager loading 上 Sequelize Bad Field Error
Sequelize Bad Field Error on Eager loading after changing association
// Include Sequelize module.
const Sequelize = require('sequelize')
// Import sequelize object,
// Database connection pool managed by Sequelize.
const sequelize = require('./database.js')
const User = require('./User.js');
const Product = require('./Product.js');
// Define method takes two arrguments
// 1st - name of table
// 2nd - columns inside the table
const FavoriteProduct = sequelize.define('favorite_product', {
favorite_id:{
// Sequelize module has INTEGER Data_Type.
type:Sequelize.INTEGER,
autoIncrement:true,
allowNull:false,
primaryKey:true
},
createdAt: Sequelize.DATE,
//no dates
}, {
updatedAt: false
});
User.hasMany(FavoriteProduct, { foreignKey: 'user_id'} );
Product.hasMany(FavoriteProduct, { foreignKey: 'product_id'} );
FavoriteProduct.belongsTo(Product, { foreignKey: 'product_id'});
FavoriteProduct.belongsTo(User, { foriegnKey: 'user_id'})
module.exports = FavoriteProduct
以前我将此模型作为 BelongsToMany 模型,通过 FavoriteProduct 具有 Product 和 User->belongToMany。我更改它是因为 FavoriteProduct 的急切加载在尝试从产品加载时不起作用。我现在有一个新问题,尝试急切加载新的 table 会抛出此错误:
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'favorite_product.userUserId' in 'field list'",
我不知道为什么会这样,但一定是因为我试图从 belongsToMany 关联切换而不进行任何迁移。数据库中没有额外的列或类似的东西。
有谁知道如何在不删除 table 的情况下解决此字段问题?
作为参考,我只是这样做的:
await Product.findOne({
include: [{
model: FavoriteProduct,
required: false,
}]
您需要更正关联 FavoriteProduct.belongsTo(User
:您拼错了选项 foreignKey
,指出 foriegnKey
。
FavoriteProduct.belongsTo(User, { foreignKey: 'user_id'})
// Include Sequelize module.
const Sequelize = require('sequelize')
// Import sequelize object,
// Database connection pool managed by Sequelize.
const sequelize = require('./database.js')
const User = require('./User.js');
const Product = require('./Product.js');
// Define method takes two arrguments
// 1st - name of table
// 2nd - columns inside the table
const FavoriteProduct = sequelize.define('favorite_product', {
favorite_id:{
// Sequelize module has INTEGER Data_Type.
type:Sequelize.INTEGER,
autoIncrement:true,
allowNull:false,
primaryKey:true
},
createdAt: Sequelize.DATE,
//no dates
}, {
updatedAt: false
});
User.hasMany(FavoriteProduct, { foreignKey: 'user_id'} );
Product.hasMany(FavoriteProduct, { foreignKey: 'product_id'} );
FavoriteProduct.belongsTo(Product, { foreignKey: 'product_id'});
FavoriteProduct.belongsTo(User, { foriegnKey: 'user_id'})
module.exports = FavoriteProduct
以前我将此模型作为 BelongsToMany 模型,通过 FavoriteProduct 具有 Product 和 User->belongToMany。我更改它是因为 FavoriteProduct 的急切加载在尝试从产品加载时不起作用。我现在有一个新问题,尝试急切加载新的 table 会抛出此错误:
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'favorite_product.userUserId' in 'field list'",
我不知道为什么会这样,但一定是因为我试图从 belongsToMany 关联切换而不进行任何迁移。数据库中没有额外的列或类似的东西。
有谁知道如何在不删除 table 的情况下解决此字段问题?
作为参考,我只是这样做的:
await Product.findOne({
include: [{
model: FavoriteProduct,
required: false,
}]
您需要更正关联 FavoriteProduct.belongsTo(User
:您拼错了选项 foreignKey
,指出 foriegnKey
。
FavoriteProduct.belongsTo(User, { foreignKey: 'user_id'})