gpt4 book ai didi

javascript - sequelize 创建或更新关联

转载 作者:行者123 更新时间:2023-12-03 22:43:07 28 4
gpt4 key购买 nike

我在使用 sequelize 在我的数据库上添加数据时遇到问题。

我有 2 个表与关联 howmany:

const Price = sequelize.define('prices', {
close: {
type: Sequelize.INTEGER
},
open: {
type: Sequelize.INTEGER
},
low: {
type: Sequelize.INTEGER
},
high: {
type: Sequelize.INTEGER
},
date: {
type: Sequelize.DATE
}
});

const Crypto = sequelize.define('cryptos', {
uuid: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV1,
primaryKey: true
},
shortname: {
type: Sequelize.STRING
},
name: {
type: Sequelize.STRING
},
totalsupling:{
type: Sequelize.STRING
},
imageurl:{
type: Sequelize.STRING
},
prooftype:{
type: Sequelize.STRING
},
premined:{
type: Sequelize.STRING
}
});

db.cryptos.hasMany(db.price, {foreignKey: 'fk_cryptoid', sourceKey: 'uuid'});
db.price.belongsTo(db.cryptos, {foreignKey: 'fk_cryptoid', targetKey: 'uuid'});

我可以添加新条目:
Crypto.create({
shortname: 'ETH',
name: 'ethereum',
totalsupling: 200000000,
rank: 2,
prices: [
{
close: '125',
open: '150',
high: '190',
low: '102'
date: new Date()
},
{
close: '125',
open: '150',
high: '190',
low: '102'
date: new Date()
}
]
}, {
include: [ Price ]
}).then(() => {
res.send("Done!");
})

但是我无法在已经存在的加密实体中添加关联的新实体价格:
Crypto.findOne({
where: {uuid: 'f0a7e0e0-793d-11e8-b166-d7ecc2040fbe'},
include: [ {model: Price }]
}).then(
function (obj) {
return obj.Price.updateAttributes(
{
close: '125',
open: '150',
high: '190',
low: '102'
}
).then(() => {
console.log('done!!')
})
});

消息错误:
Unhandled rejection TypeError: Cannot read property 'updateAttributes' of undefined
at /home/blind/Documents/Init-Site-Info-Crypto/Controller/init.controller.js:65:29
at tryCatcher (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)

最佳答案

在您的协会中,加密货币具有 许多 价格。因此,如果您在加密中包含您的价格模型,它应该返回一个价格数组,并且您不能直接在该数组中执行 updateAttributes。这是一个问题。另一个问题是您的代码无法关联表。

尝试为您的关联使用别名。像这样的东西:

db.cryptos.hasMany(db.price, {as: 'prices', foreignKey: 'fk_cryptoid', sourceKey: 'uuid'}); 
db.price.belongsTo(db.cryptos, {as: 'crypto', foreignKey: 'fk_cryptoid', targetKey: 'uuid'});

然后你可以访问像
Crypto.findOne({
where: {uuid: 'f0a7e0e0-793d-11e8-b166-d7ecc2040fbe'},
include: [{model: Price, as: 'prices'}]
})
.then(function (prices) {
console.log(prices)
})

关于javascript - sequelize 创建或更新关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045087/

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