最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sequelize 错误:belongsTo 使用不是 Sequelize.Model 的子类的东西调用

网站源码admin34浏览0评论

Sequelize 错误:belongsTo 使用不是 Sequelize.Model 的子类的东西调用

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 时遇到错误

我认为所有外键都是正确的,我还遗漏了哪些其他错误?

当我在产品模型中添加关联时抛出错误

回答如下:
发布评论

评论列表(0)

  1. 暂无评论