I'm reading through the Sequelize docs for version 6, and it has a section for methods included with different types of associations. I'm going through the methods for .hasOne association and it isn't working like it should. The code I've written should output this to the console:
null
some-bar
yet-another-bar
null
However this is what I am getting:
null
some-bar
some-bar
yet-another-bar
Here is my code:
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()
I've copy pasted the code inside my create function directly from the docs so I'm not sure what's going wrong. Any help would be appreciated.
In this piece of code, the documentation goes wrong.
// 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
You can check this yourself by setting a breakpoint on the first line of code, stepping line by line and checking the bar table records after each line.
P.S. To get the correct example we need to insert
await foo.setBar(null); // Un-associate
right before
await foo.createBar({ name: 'yet-another-bar' });