我正在尝试使用sequelize-cli创建数据库并建立hasMany关联,但是由于某些原因,我无法在第二张表中填充数据
我在考虑该关联是否真的创建,在我的sgbd上我看不到“键”,即外键表示形式; 我正在显示以下视频:https://www.youtube.com/watch?v=9xJLcTxlEIs 但只有我最喜欢的工作,只要显示此错误即可:
Listening on port 5000
Executing (default): INSERT INTO `categoria` (`id`,`nomept`,`nomeen`,`nomefr`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?);
Unhandled rejection TypeError: cate.createFaqs is not a function
at Categoria.create.then.cate (/home/coyas/software_center/wifyanywhere/server.js:13:10)
at tryCatcher (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/promise.js:517:31)
at Promise._settlePromise (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/promise.js:574:18)
at Promise._settlePromise0 (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/promise.js:619:10)
at Promise._settlePromises (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/promise.js:699:18)
at _drainQueueStep (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/coyas/software_center/wifyanywhere/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
我的服务器代码:
const Categoria = require('./models').categoria
const Faqs = require('./models').faqs
Categoria.create({
nomept: 'terrs',
nomeen: 'sqsdqs',
nomefr: 'fdds'
}).then( cate => {
cate.createfaqs({
title: "teste terra",
descricao: "dqsdsddqs dsd qd qd qdqq"
}).then( () => console.log('funcionou'))
})
其他代码:
我希望cate.createFaqs()能够正常工作,并将一些数据放入categoriaId属于表categoria的表中
答案 0 :(得分:0)
我解决了这个问题,只需要添加以下代码:
参考:{ 型号:“类别”, 密钥:“ id” }
在迁移代码中,瞧!