【问题标题】:sequelize create or update with associationsequelize 创建或更新关联
【发布时间】:2018-06-26 14:15:04
【问题描述】:

我在使用 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)

【问题讨论】:

  • 有没有可以显示的输出?
  • 我已经编辑了这篇文章

标签: javascript node.js sequelize.js


【解决方案1】:

在您的协会中,加密货币有许多价格。所以,如果你在加密中包含你的价格模型,它应该返回一个价格数组,你不能直接在数组中执行 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'});

然后就可以访问like了

Crypto.findOne({
    where: {uuid: 'f0a7e0e0-793d-11e8-b166-d7ecc2040fbe'},
    include: [{model: Price, as: 'prices'}]
})
.then(function (prices) {
   console.log(prices)
})

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2020-08-25
    相关资源
    最近更新 更多