子类的东西调用"/>
Sequelize 错误:belongsTo 使用不是 Sequelize.Model 的子类的东西调用
我试图在 Sequelize NodeJS 中关联模型并遇到问题,这是我定义的两个模型。
/* eslint-disable @typescript-eslint/no-unused-vars */
'use strict';
import Sequelize from 'sequelize';
import db from '../db';
import Review from './Review';
const Product = db.define(
'Product',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
price: {
type: Sequelize.FLOAT,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false
},
description: {
type: Sequelize.STRING
},
countInStock: {
type: Sequelize.INTEGER,
allowNull: false
},
numReviews: {
type: Sequelize.INTEGER,
allowNull: false
},
image: {
type: Sequelize.STRING,
allowNull: false
},
category: {
type: Sequelize.STRING,
allowNull: false
},
rating: {
type: Sequelize.FLOAT,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW
}
},
{
timestamps: true
}
);
Product.hasMany(Review, {
foreignKey: 'productId',
as: 'reviews'
});
export default Product;
和
import Sequelize from 'sequelize';
import db from '../db';
import User from './User';
import Product from './Product';
const Review = db.define(
'Review',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
rating: {
type: Sequelize.FLOAT,
allowNull: false
},
comment: {
type: Sequelize.STRING,
allowNull: false
},
userId: {
type: Sequelize.STRING,
allowNull: false,
references: {
model: User,
key: 'id'
}
},
productId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Product,
key: 'id'
}
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW
}
},
{
timestamps: true,
underscored: true
}
);
Review.belongsTo(Product, {
foreignKey: 'productId',
as: 'product'
});
Review.belongsTo(User, {
foreignKey: 'userId',
as: 'user'
});
export default Review;
我在用 not thats not a subclass of Sequelize Model 调用 belongsTo 时遇到错误
我认为所有外键都是正确的,我还遗漏了哪些其他错误?
当我在产品模型中添加关联时抛出错误
回答如下:更多推荐
Sequelize 错误:belongsTo 使用不是 Sequelize.Model 的子类的东西调用
发布评论