Sequelize createBar 方法未按预期工作

Sequelize createBar Method not Working as Expected

我正在阅读第 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()

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

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

  // 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

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

P.S。为了得到正确的例子,我们需要插入

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

就在

之前
await foo.createBar({ name: 'yet-another-bar' });