Sequelize Associations Error Users.hasMany called with something that's not a subclass of Sequelize.Model. when associating between 2 models

huangapple go评论97阅读模式
英文:

Sequelize Associations Error Users.hasMany called with something that's not a subclass of Sequelize.Model. when associating between 2 models

问题

我在关联两个模型(Users 和 Comments)时遇到了一个错误:“Users.hasMany 调用的参数不是 Sequelize.Model 的子类。” 但是当我移除 const Users = require("./user");Comments.belongsTo(Users, { foreignKey: "userId", onDelete: "cascade" }); 这两行代码时,我会得到一个错误:“Users 未与 Comments 关联!” 我已经在用户模型中进行了关联。

这是我的Comments模型:

  1. const { DataTypes } = require("sequelize");
  2. const { sequelize } = require(".");
  3. const Users = require("./user");
  4. const Comments = sequelize.define("Comments", {
  5. comment: {
  6. type: DataTypes.STRING,
  7. allowNull: false,
  8. validate: {
  9. min: 1,
  10. max: 50,
  11. },
  12. },
  13. });
  14. // 如果我从代码中移除这部分,我会得到“Users 未与 Comments 关联!”的错误。
  15. Comments.belongsTo(Users, {
  16. foreignKey: "userId",
  17. onDelete: "cascade",
  18. });
  19. //
  20. module.exports = Comments;

这是我的Users模型:

  1. const { DataTypes } = require("sequelize");
  2. const { sequelize } = require(".");
  3. const Comments = require("./comment");
  4. const jwt = require("jsonwebtoken");
  5. const Users = sequelize.define("Users", {
  6. username: {
  7. type: DataTypes.STRING,
  8. allowNull: false,
  9. unique: true,
  10. validate: {
  11. min: 5,
  12. max: 30,
  13. notNull: true,
  14. },
  15. },
  16. });
  17. Users.hasMany(Comments, {
  18. foreignKey: "userId",
  19. onDelete: "cascade",
  20. });
  21. module.exports = Users;

我还有一个用于检索所有评论的 GET 请求,我想在其中包括 Users 表,以便我可以访问创建该评论的用户的用户名:

  1. const getComments = async (req, res) => {
  2. const comments = await Comments.findAll({
  3. include: Users,
  4. });
  5. res.send(comments);
  6. };
英文:

I am encountering an error Users.hasMany called with something that's not a subclass of Sequelize.Model. when associating between 2 models (Users and Comments). BUT when i remove const Users = require("./user"); and Comments.belongsTo(Users, { foreignKey: "userId", onDelete: "cascade", });
from the Comments model i instead get "Users is not associated to Comments!" error. I already associated it in the users model.

This is my model for Comments:

  1. const { DataTypes } = require("sequelize");
  2. const { sequelize } = require(".");
  3. const Users = require("./user");
  4. const Comments = sequelize.define("Comments", {
  5. comment: {
  6. type: DataTypes.STRING,
  7. allowNull: false,
  8. validate: {
  9. min: 1,
  10. max: 50,
  11. },
  12. },
  13. });
  14. // If i remove this from the code i instead get Users is not associated to Comments!
  15. Comments.belongsTo(Users, {
  16. foreignKey: "userId",
  17. onDelete: "cascade",
  18. });
  19. //
  20. module.exports = Comments;

This is my Users model:

  1. const { DataTypes } = require("sequelize");
  2. const { sequelize } = require(".");
  3. const Comments = require("./comment");
  4. const jwt = require("jsonwebtoken");
  5. const Users = sequelize.define("Users", {
  6. username: {
  7. type: DataTypes.STRING,
  8. allowNull: false,
  9. unique: true,
  10. validate: {
  11. min: 5,
  12. max: 30,
  13. notNull: true,
  14. },
  15. },
  16. });
  17. Users.hasMany(Comments, {
  18. foreignKey: "userId",
  19. onDelete: "cascade",
  20. });
  21. module.exports = Users;

I also have a GET request for retrieving all comments where I would like to include the Users table so that I have access to the username of the user that created that comment:

  1. const getComments = async (req, res) => {
  2. const comments = await Comments.findAll({
  3. include: Users,
  4. });
  5. res.send(comments);
  6. };

答案1

得分: 1

你可以定义一个单独的全局方法来处理关联:

  1. const Users = require("./user");
  2. const Comments = require("./comment");
  3. const sequelize = new Sequelize(...);
  4. export const sync = async () => {
  5. Comments.belongsTo(Users, {
  6. foreignKey: "userId",
  7. onDelete: "cascade",
  8. });
  9. Users.hasMany(Comments, {
  10. foreignKey: "userId",
  11. onDelete: "cascade",
  12. });
  13. await sequelize.sync();
  14. }

然后,在初始化数据库/应用程序时使用它:

  1. import { sync } from './path/to/sync';
  2. await sync();

这将确保在关联时两个模型都已定义。

英文:

You could define a separate global method for handling associations:

  1. const Users = require("./user");
  2. const Comments = require("./comment");
  3. const sequelize = new Sequelize(...);
  4. export const sync = async () => {
  5. Comments.belongsTo(Users, {
  6. foreignKey: "userId",
  7. onDelete: "cascade",
  8. });
  9. Users.hasMany(Comments, {
  10. foreignKey: "userId",
  11. onDelete: "cascade",
  12. });
  13. await sequelize.sync();
  14. }

Then, use it when initializing the database/application:

  1. import { sync } from './path/to/sync';
  2. await sync();

This will ensure both models are defined when associated.

huangapple
  • 本文由 发表于 2023年6月16日 01:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定