【问题标题】:Sequelize createBar Method not Working as ExpectedSequelize createBar 方法未按预期工作
【发布时间】:2022-02-03 06:15:06
【问题描述】:

我正在阅读第 6 版的 Sequelize 文档,其中有一节介绍包含在不同类型关联中的方法。我正在浏览 .hasOne 关联的方法,但它没有按应有的方式工作。我写的代码应该输出到控制台:

null
some-bar
yet-another-bar
null

但这是我得到的:

null
some-bar
some-bar
yet-another-bar

这是我的代码:

const Sequelize = require('Sequelize')
const db = new Sequelize('postgres://localhost:5432/sqlstuff', {
  logging: false
})

const Foo = db.define('foo', {
  name: Sequelize.STRING
}, { timestamps: false })

const Bar = db.define('bar', {
  name: Sequelize.STRING
}, { timestamps: false })

Foo.hasOne(Bar)

const create = async function() {
  const foo = await Foo.create({ name: 'the-foo' });
  const bar1 = await Bar.create({ name: 'some-bar' });
  const bar2 = await Bar.create({ name: 'another-bar' });
  console.log(await foo.getBar()); // null
  await foo.setBar(bar1);
  console.log((await foo.getBar()).name); // 'some-bar'
  await foo.createBar({ name: 'yet-another-bar' });
  const newlyAssociatedBar = await foo.getBar();
  console.log(newlyAssociatedBar.name); // 'yet-another-bar'
  await foo.setBar(null); // Un-associate
  console.log((await foo.getBar()).name); // null
}


const connect = async function() {
  try {
    await db.sync({ force: true })
    await create()
    await db.close()
  } catch (error) {
    console.error(error)
  }
}

connect()

我直接从文档中复制粘贴了我的 create 函数中的代码,所以我不确定出了什么问题。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    在这段代码中,文档出错了。

      // after this line we have a record only in foo table with the name 'the-foo'
      const foo = await Foo.create({ name: 'the-foo' });
      // after this line we have a record in bar table with the name 'some-bar' and with no link to the foo record
      const bar1 = await Bar.create({ name: 'some-bar' });
      // after this line we have another record in bar table with the name 'another-bar' and with no link to the foo record
      const bar2 = await Bar.create({ name: 'another-bar' });
      // this output is correct. We don't have any bar records with a link to the foo record yet
      console.log(await foo.getBar()); // null
      // now we set a link between 'some-bar' record and the foo record (by setting fooId in this bar record)
      await foo.setBar(bar1);
      // this output is correct. We have 'some-bar' record linked to the foo record
      console.log((await foo.getBar()).name); // 'some-bar'
      // PAY ATTENTION: this line does NOT clear the previous link in 'some-bar' record, after executing it we will have TWO records linked to the foo record: 'some-bar' and 'yet-another-bar'
      await foo.createBar({ name: 'yet-another-bar' });
      // here we will get either 'some-bar' or 'yet-another-bar' record (depends on what record will return PostgreSQL as the first one)
      const newlyAssociatedBar = await foo.getBar();
      // this output may or may NOT be correct depending on what linked records out of two ones linked to the foo record will be returned first.
      console.log(newlyAssociatedBar.name); // 'yet-another-bar' or 'some-bar'
      // here we clear ALL (two in our case) links in bar records to the foo record
      await foo.setBar(null); // Un-associate
      // correct output. We have three records in the bar table and all of them are with null fooId column values.
      console.log((await foo.getBar()).name); // null
    

    您可以通过在代码的第一行设置断点,逐行执行并检查每行之后的条形表记录来自行检查。

    附:为了得到正确的例子,我们需要插入

      await foo.setBar(null); // Un-associate
    

    就在之前

    await foo.createBar({ name: 'yet-another-bar' });
    
    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 2019-04-22
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2020-10-04
    相关资源
    最近更新 更多