关联创建或更新

时间:2018-06-26 14:15:04

标签: javascript node.js sequelize.js

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

我有2个表,具有多个关联:

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)

1 个答案:

答案 0 :(得分:0)

在您的协会中,加密货币具有许多价格。因此,如果您在加密货币中包含价格模型,则该价格模型应返回一个价格数组,并且您不能直接在该数组中执行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)
})