错误:调用model.belongsTo时使用了不是Sequelize.Model子类的内容。

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

Error: model.belongsTo called with something that's not a subclass of Sequelize.Model

问题

我有两个模型categorysub_category,我已经实现了它们之间的一对多关联,但我得到了类似于sub category Error: sub_category.belongsTo called with something that's not a subclass of Sequelize.Model的错误。为什么?

category 模型

const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const subcat = require("./subCategory");

const categoryModel = {
  category_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  category_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let category = null;
const initCategoryModel = async () => {
  try {
    if (category) return category;
    const sequelize = await getConnection();
    category = sequelize.define("category", categoryModel, {
      freezeTableName: true,
    });

    category.hasMany(subcat, {
      foreignKey: "category_id",
      onDelete: "CASCADE",
    });

    await category.sync({ alter: true });
    return category;
  } catch (err) {
    console.log("category", err);
  }
};

module.exports = { initCategoryModel };

sub category 模型

const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const category = require("./category");

const subcatModel = {
  subcat_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  subcat_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let subcat = null;
const initSubcatModel = async () => {
  try {
    if (subcat) return subcat;
    const sequelize = await getConnection();
    subcat = sequelize.define("sub_category", subcatModel, {
      freezeTableName: true,
    });

    subcat.belongsTo(category, {
      foreignKey: "category_id",
      onDelete: "CASCADE",
    });

    await subcat.sync({ alter: true });
    return subcat;
  } catch (err) {
    console.log("sub category", err);
  }
};

module.exports = { initSubcatModel };

这里category拥有多个sub-category,而sub-category属于一个category

英文:

I have two models category and sub_category and i had implemented the one-to-many association b/w them but i get the error somthing like sub category Error: sub_category.belongsTo called with something that's not a subclass of Sequelize.Model. why?

category model

const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const subcat = require("./subCategory");

const categoryModel = {
  category_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  category_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let category = null;
const initCategoryModel = async () => {
  try {
    if (category) return category;
    const sequelize = await getConnection();
    category = sequelize.define("category", categoryModel, {
      freezeTableName: true,
    });

    category.hasMany(subcat, {
      foreignKey: "category_id",
      onDelete: "CASCADE",
    });

    await category.sync({ alter: true });
    return category;
  } catch (err) {
    console.log("category", err);
  }
};

module.exports = { initCategoryModel };

sub category model

const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const category = require("./category");

const subcatModel = {
  subcat_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  subcat_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let subcat = null;
const initSubcatModel = async () => {
  try {
    if (subcat) return subcat;
    const sequelize = await getConnection();
    subcat = sequelize.define("sub_category", subcatModel, {
      freezeTableName: true,
    });

    subcat.belongsTo(category, {
      foreignKey: "category_id",
      onDelete: "CASCADE",
    });

    await subcat.sync({ alter: true });
    return subcat;
  } catch (err) {
    console.log("sub category", err);
  }
};

module.exports = { initSubcatModel };

here category has many sub-category and sub-category belongs to a category,

答案1

得分: 1

从你粘贴的代码中可以看出,你似乎没有导出 subcat 或 category,而是导出了 init 函数。

const subcat = require("./subCategory"); // { initSubcatModel } and not a model

由于存在循环依赖,无论如何都会有问题,具体取决于 init 函数调用的顺序。可能会导致 subcat 或 category 未定义(因为对应的 init 函数尚未调用)。

你应该将模型定义、模型关联和模型同步分成三个独立的步骤,确保模型定义首先被调用,然后进行关联,最后进行同步。

// category.js
// ...

const getCategoryModel = async () => {
  // ...
};

const associateCategoryModel = () => {
  // ...
};

module.exports = { getCategoryModel, associateCategoryModel };
// subcategory.js
// ...

const getSubcatModel = async () => {
  // ...
};

const associateSubcatModel = () => {
  // ...
};

module.exports = { getSubcatModel, associateSubcatModel };
// index.js
const initModels = async () => {
  // ...
};
英文:

From the code you pasted, you don't seems to export subcat nor category but the init functions.

const subcat = require("./subCategory"); // { initSubcatModel } and not a model

And since there is a circular dependency, you would have an issue anyway, depending in the order in which init function is called, you would have subcat or category be undefined (since corresponding init function has not been called yet).

You should rather split the model definition, the model association and the model sync in three separate steps
Ensure that are model definition is called first, then do associations and finally sync.

// category.js
const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const { getSubcatModel } = require("./subcat");

const categoryModel = {
  category_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  category_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let category = null;
const getCategoryModel = () => {
  try {
    if (category) return category;
    const sequelize = await getConnection();
    category = sequelize.define("category", categoryModel, {
      freezeTableName: true,
    });

    return category;
  } catch (err) {
    console.log("category", err);
  }
};
const associateCategoryModel = () => {
  const category = getCategoryModel();
  const subcat = getSubcatModel();
  category.hasMany(subcat, {
    foreignKey: "category_id",
    onDelete: "CASCADE",
  });
}

module.exports = { getCategoryModel, associateCategoryModel };
// subcategory.js
const { DataTypes } = require("sequelize");
const { getConnection } = require("../helper/databaseConnection");
const { getCategoryModel } = require("./category");

const subcatModel = {
  subcat_id: {
    primaryKey: true,
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
  },
  subcat_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
};

let subcat = null;
const getSubcatModel = () => {
  try {
    if (subcat) return subcat;
    const sequelize = await getConnection();
    subcat = sequelize.define("sub_category", subcatModel, {
      freezeTableName: true,
    });

    return subcat;
  } catch (err) {
    console.log("sub category", err);
  }
};

const associateSubcatModel = () => {
  const subcat = getSubcatModel();
  const category = getCategoryModel();
  subcat.belongsTo(category, {
    foreignKey: "category_id",
    onDelete: "CASCADE",
  });
}

module.exports = { getSubcatModel, associateSubcatModel };
// index.js

const initModels = async () => {
  const category = getCategoryModel();
  const subcat = getSubcatModel();

  associateCategoryModel();
  associateSubcatModel();

  await category.sync();
  await subcat.sync();
}

huangapple
  • 本文由 发表于 2023年6月8日 18:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431050.html
匿名

发表评论

匿名网友

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

确定