[英]The sequelize many-to-many setter does not exist
我试图通过 Sequalize 定义多对多关系,但我似乎无法在创建元素后掌握应用关系的窍门。
多对多关系是LegalFoundation
和Case
之间的关系,并且LegalCase
已在数据库中定义,因此似乎belongsToMany()
完成了它的工作。
但是,在确保定义了所有LegalFoundations
并创建了Case
之后,我正在努力定义两者之间的关系。
似乎created_case
缺少所需的 function: UnhandledPromiseRejectionWarning: TypeError: created_case.addLegalFoundation is not a fun n is not a function
。 但是,我读过的所有文档都表明, create
function 应该返回正确的元素。 所以我有点难过。
如果这对调试有任何帮助,则 GET 请求返回column legal_foundations->LegalCase.case_id does not exist
。
希望有人能给我指路!
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
}
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
应指示 model 的字段,您调用其方法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_Foundation
或addLegal_foundation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.