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

编程入门 行业动态 更新时间:2024-10-04 21:20:27

Sequelize 错误:belongsTo 使用不是 Sequelize.Model 的<a href=https://www.elefans.com/category/jswz/34/1770569.html style=子类的东西调用"/>

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 的子类的东西调用

本文发布于:2024-05-30 18:31:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   错误   东西   Sequelize   Model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!