[英]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.