gpt4 book ai didi

javascript - sequelize 多对多 setter 不存在

转载 作者:行者123 更新时间:2023-12-03 22:32:04 25 4
gpt4 key购买 nike

我试图通过 Sequalize 定义多对多关系,但我似乎无法在创建元素后应用这种关系。
多对多关系是在 LegalFoundationCase 之间,并且 LegalCase 已经在数据库中定义,所以看起来 belongsToMany() 完成了它的工作。
但是,在确保定义了所有 LegalFoundations 并创建了 Case 之后,我正在努力定义两者之间的关系。
似乎 created_case 缺少所需的功能: UnhandledPromiseRejectionWarning: TypeError: created_case.addLegalFoundation is not a fun n is not a function 。但是,我读过的所有文档都表明 create 函数应该返回正确的元素。所以我有点难住了。
如果这对调试有帮助,GET 请求将返回 column legal_foundations->LegalCase.case_id does not exist
希望有人能给我指路!
数据库.js

const Sequelize = require('sequelize')

const database = new Sequelize('#####', '#####', '#####', {
dialect: 'postgres',
operatorsAliases: Sequelize.Op
})

const District = database.define('district', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: { type: Sequelize.STRING, allowNull: false}
});

const LegalFoundation = database.define('legal_foundation', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
text: {type: Sequelize.TEXT, allowNull: false}
});

const Case = database.define('case', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
ref: {type: Sequelize.TEXT},
report: {type: Sequelize.ARRAY(Sequelize.TEXT)},
consideration: {type: Sequelize.ARRAY(Sequelize.TEXT)},
decision: {type: Sequelize.ARRAY(Sequelize.TEXT)},
date: {type: Sequelize.DATE}
});

District.hasMany(Case)
LegalFoundation.belongsToMany(Case, {through: 'LegalCase', as: 'cases', foreignKey: "case_id"})
Case.belongsToMany(LegalFoundation, {through: 'LegalCase', as: 'legal_foundations', foreignKey: "legal_foundation_id"})

module.exports = {
database,
District,
LegalFoundation,
Case
}
案例.js
const express = require('express')
const { Case, District, LegalFoundation } = require('./db/database')
const { Op, Sequelize } = require("sequelize");

const router = express.Router()

router.post('/', async (req, res, next) => {
try {
const {ref, report, consideration, decision, legal_foundation, date, district} = req.body;
const [district_ins, created_d] = await District.findOrCreate({
where: {
name: district
},
});
foundations = [];
if (typeof legal_foundation == 'object'){
legal_foundation.forEach(async element => {
let [f, created_f] = await LegalFoundation.findOrCreate({
where: {
text: element
}
});
foundations.push(f.id)
});
} else {
let [f, created_f] = await LegalFoundation.findOrCreate({
where: {
text: legal_foundation
}
});
foundations.push(f.id)
}
const created_case = await Case.create({
ref: ref,
report: report,
consideration: consideration,
decision: decision,
date: date,
districtId: district_ins.id
});

foundations.forEach(async element => {
await created_case.addLegalFoundation(element);
});

res.json({success: true, id})
}
catch (error) {
res.json({success: false, error: error})
}
})

router.get('/', async (req, res, next) => {
try{
const all = await Case.findAll({include: {
model: LegalFoundation,
as: 'legal_foundations'
}});
res.json({success: true, item: all});
}
catch (error) {
res.json({success: false, error: error.message})
}
})

module.exports = router;

最佳答案

你在这里有几个问题:

  • 不正确的 belongsToMany 关联。 foreignKey 应指示您调用其方法 belongsToMany 的模型的字段:

  • LegalFoundation.belongsToMany(Case, {through: 'LegalCase', as: 'cases',
    foreignKey: "legal_foundation_id", otherKey: "case_id" })
    Case.belongsToMany(LegalFoundation, {through: 'LegalCase', as: 'legal_foundations',
    foreignKey: "case_id", otherKey: "legal_foundation_id"})
  • forEach 方法不能等待异步迭代。你应该使用 for of :
  • for (const element of legal_foundation) {
    let [f, created_f] = await LegalFoundation.findOrCreate({
    where: {
    text: element
    }
    });
    foundations.push(f.id)
    }
    for (const element of foundations) {
    await created_case.addLegalFoundation(element);
    }
  • 我想是因为你使用 legal_foundations 名称和 _ 然后尝试查看 created_case 的方法,也许它们应该像 addLegal_FoundationaddLegal_foundation
  • 关于javascript - sequelize 多对多 setter 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66026510/

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