sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use auto generated functions ?

cth166 opened this issue · comments

commented

How to use auto generated functions (such as add Bar) in many-to-many relationship?

const Foo = sequelize.define('Foo', { name: DataTypes.TEXT });
const Bar = sequelize.define('Bar', { name: DataTypes.TEXT });
Foo.belongsToMany(Bar, { through: 'Foo_Bar' });
Bar.belongsToMany(Foo, { through: 'Foo_Bar' });

await sequelize.sync();
const foo = await Foo.create({ name: 'foo' });
const bar = await Bar.create({ name: 'bar' });
await foo.addBar(bar);
const fetchedFoo = await Foo.findOne({ include: Bar });

I want to insert one in junction table in my demo.

I try to use $add but error
TypeError: this[("add" + (0 , string_1.capitalize)(...))] is not a function at c.$add (C:\Users\cth16\Desktop\mysql2\node_modules\sequelize-typescript\dist\model\model\model.js:26:67) at C:\Users\cth16\Desktop\mysql2\dist\index.js:1:2970 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
image-20230225043757818

You can try author.addBook(book) or await author.addBook(book, { through: { status: 'unread' }})

You can try author.addBook(book) or await author.addBook(book, { through: { status: 'unread' }})

Thank you. I haven't login github in a long time. I reviewed the document a few days ago. And I didn't meet this problem again.