NodeJS在声明Sequelize 6多对多关联模型时相互导入文件
NodeJS import each other file when stating model of Sequelize 6 Many To Many associations
我对 sequelize 6 模型定义有疑问。
我需要创建一个具有多对多关联的模型,即产品到类别。
一个商品可以有多个分类,一个分类可以有多个商品。因此,我创建了一个名为 category_products.
的支点 table
我创建模型的方法是每个模型一个文件。因此每个模型将有三个文件,即 Product、Category 和 CategoryProduct。
我在为每个产品和类别模型定义关联时遇到的问题。它的发生是因为产品模型文件(product.model.js)正在导入类别模型文件(category.model.js)以定义与产品的多对多关联。类别模型及其文件也在做同样的事情,导入 product.model.js 以定义与产品的多对多关联。
因此我的代码返回
belongsToMany called with something that's not a subclass of
Sequelize.Model
发生此错误是因为在运行时,为定义多对多关联而导入的模型变量没有返回模型,而是 'undefined'。
这里的代码可以让你更了解我面临的问题:
// category_product.model.js
const CategoryProduct = sequelize.define('CategoryProduct', {}, {
tableName : 'category_products',
underscored: true,
});
export default CategoryProduct;
.
// product.model.js
import Category from "./category.model";
import CategoryProduct from "./category_product.model";
const Product = sequelize.define('Product', {}, {
tableName : 'products',
underscored: true,
});
Product.belongsToMany(Category, {
through: CategoryProduct,
foreignKey: 'product_id',
otherKey: 'category_id'
})
export default Product;
.
// category.model.js
import Product from "./product.model";
import CategoryProduct from "./category_product.model";
const Category = sequelize.define('Category', {}, {
tableName : 'categories',
underscored: true,
});
// here the problem. im using babel, 'Product' variable is returning undefined
// the hypothesis that came to my mind why this is happening because
// this file (category.model.js) is including product.model.js
// and product.model.js is including this file too.
// this is lead to infinity cycle of file include. and because of this,
// babel returning 'undefined' instead
// and this issue is happening to product.model.js too
Category.belongsToMany(Product, {
through: CategoryProduct,
foreignKey: 'category_id',
});
export default Category;
我知道我可以通过创建一个附加文件并将每个模型的关联定义放在那里来解决这个问题,但我不想这样。我想为一个文件保留一个模型。
有适合我的解决方案吗?
谢谢大家。
您应该将关联定义为可以在所有模型注册后单独调用的函数。参见
我对 sequelize 6 模型定义有疑问。
我需要创建一个具有多对多关联的模型,即产品到类别。
一个商品可以有多个分类,一个分类可以有多个商品。因此,我创建了一个名为 category_products.
的支点 table我创建模型的方法是每个模型一个文件。因此每个模型将有三个文件,即 Product、Category 和 CategoryProduct。
我在为每个产品和类别模型定义关联时遇到的问题。它的发生是因为产品模型文件(product.model.js)正在导入类别模型文件(category.model.js)以定义与产品的多对多关联。类别模型及其文件也在做同样的事情,导入 product.model.js 以定义与产品的多对多关联。
因此我的代码返回
belongsToMany called with something that's not a subclass of Sequelize.Model
发生此错误是因为在运行时,为定义多对多关联而导入的模型变量没有返回模型,而是 'undefined'。
这里的代码可以让你更了解我面临的问题:
// category_product.model.js
const CategoryProduct = sequelize.define('CategoryProduct', {}, {
tableName : 'category_products',
underscored: true,
});
export default CategoryProduct;
.
// product.model.js
import Category from "./category.model";
import CategoryProduct from "./category_product.model";
const Product = sequelize.define('Product', {}, {
tableName : 'products',
underscored: true,
});
Product.belongsToMany(Category, {
through: CategoryProduct,
foreignKey: 'product_id',
otherKey: 'category_id'
})
export default Product;
.
// category.model.js
import Product from "./product.model";
import CategoryProduct from "./category_product.model";
const Category = sequelize.define('Category', {}, {
tableName : 'categories',
underscored: true,
});
// here the problem. im using babel, 'Product' variable is returning undefined
// the hypothesis that came to my mind why this is happening because
// this file (category.model.js) is including product.model.js
// and product.model.js is including this file too.
// this is lead to infinity cycle of file include. and because of this,
// babel returning 'undefined' instead
// and this issue is happening to product.model.js too
Category.belongsToMany(Product, {
through: CategoryProduct,
foreignKey: 'category_id',
});
export default Category;
我知道我可以通过创建一个附加文件并将每个模型的关联定义放在那里来解决这个问题,但我不想这样。我想为一个文件保留一个模型。 有适合我的解决方案吗? 谢谢大家。
您应该将关联定义为可以在所有模型注册后单独调用的函数。参见