Estoy leyendo los documentos de Sequelize para la versión 6, y tiene una sección para métodos incluidos con diferentes tipos de asociaciones. Estoy revisando los métodos para la asociación .hasOne y no funciona como debería. El código que he escrito debería enviar esto a la consola:
null some-bar yet-another-bar nullSin embargo, esto es lo que obtengo:
null some-bar some-bar yet-another-barAquí está mi código:
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()Copié y pegué el código dentro de mi función de creación directamente desde los documentos, así que no estoy seguro de qué está pasando. Cualquier ayuda sería apreciada.
En este fragmento de código, la documentación falla.
// 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); // nullPuede verificar esto usted mismo estableciendo un punto de interrupción en la primera línea de código, avanzando línea por línea y verificando los registros de la tabla de barras después de cada línea.
PS Para obtener el ejemplo correcto, necesitamos insertar
await foo.setBar(null); // Un-associatejusto antes de
await foo.createBar({ name: 'yet-another-bar' });