gpt4 book ai didi

node.js - 为什么此代码无法创建 Sequelize 关联

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

我正在尝试使用 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'))
})

其他代码:
  • faqs.js: https://pastebin.com/4ww06n8k
  • 类别.js: https://pastebin.com/c8gD2GMG
  • 迁移/...faqs.js:https://pastebin.com/kWYMxQxf
  • 迁移/...categoria.js: https://pastebin.com/5J8ZYnAS

  • 我希望 cate.createFaqs() 工作并将一些数据放入表中,categoriaId 属于表类别

    最佳答案

    我解决了这个问题,只需要添加这行代码:

    引用: {
    模型:'类别',
    键:'身份证'
    },

    在迁移代码中,瞧!

    关于node.js - 为什么此代码无法创建 Sequelize 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891930/

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