gpt4 book ai didi

mysql - SequelizeEagerLoadingError : product is not associated to collection

转载 作者:行者123 更新时间:2023-12-03 22:34:46 27 4
gpt4 key购买 nike

我在 Mysql 中使用 sequelize ORM。我有 3 个模型:Product、Collection、CollectionProduct

Product 和 Collection 之间的关系是多对多的,为了在 sequelize 中处理这个问题,我使用了 belongsToMany 关联。一切都很好,但是当我运行此代码以使用 获取包含 的产品的集合时,会出现此错误:

SequelizeEagerLoadingError:产品与收藏无关!

产品型号:

module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define('product', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name_en: {
type: Sequelize.STRING(255),
},
description_en: {
type: Sequelize.STRING(1024),
},
price: {
type: Sequelize.FLOAT,
allowNull: false,
},
type: {
type: Sequelize.STRING(255),
},
height: {
type: Sequelize.INTEGER,
},
width: {
type: Sequelize.INTEGER,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
}, {})

Product.associate = (models) => {
Product.belongsToMany(models.collection, { through: models.collectionProduct, as: 'collections', foreignKey: 'productId' })
}

return Product
}

集合模型:
module.exports = (sequelize, Sequelize) => {
const Collection = sequelize.define(
'collection', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
},
name_en: {
type: Sequelize.STRING(255),
},
itemsCount: {
type: Sequelize.INTEGER,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
}, {},
)
Collection.associate = (models) => {
Collection.belongsToMany(models.product, { through: models.collectionProduct, as: 'products', foreignKey: 'collectionId' })
}
return Collection
}

系列产品型号:
module.exports = (sequelize, Sequelize) => {
const CollectionProduct = sequelize.define('collectionProduct', {
collectionId: {
type: Sequelize.INTEGER,
},
productId: {
type: Sequelize.INTEGER,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
}, {})

CollectionProduct.associate = (models) => {}


return CollectionProduct
}

路线/collection.js
const express = require('express')

const router = express.Router()

const Collection = require('../../controllers/collectionController')

router.get('/:id', async (req, res) => {
const { id: collectionId } = req.params
const collection = await Collection.getOne(collectionId)
return collection
}

collectionController
const db = require('../models/index')

const Collection = db.collection
const Product = db.product

const getOne = async (collectionId) => {
const collection = await Collection.findByPk(collectionId, {
include: {
model: Product,
as: 'products',
attributes: ['id', 'name_en'],
},
})
return collection
}

最佳答案

我发现了我的问题。在 collectionController 我使用模型:产品
和产品应该是一个 Sequelize 模型。但在我的代码中,这是一个在模型/索引中调用的函数。所以我改变了我的电话并将 Sequelize 模型传递给 getOne

关于mysql - SequelizeEagerLoadingError : product is not associated to collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62370846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com