Friday 21 February 2020

Sequelize - having trouble creating self referenced M:N relationships

I am trying to allow my users to block other users and while it is an easy thing to do in pure SQL (jjust create a Blocks table with two foreign keys) it is a pain in Sequelize.​My user model is:const User = sequelize.define('Users', {UserID: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true},//snipped attributes that are not part of the primary key});Then I doUser.belongsToMany(User, {through: 'BlockedUsers', as: "Blocks"});which does not result in the BlockedUsers table being created. In fact NOTHING changes in the DB structure doing that.I have also tried doing a "two-way" relationship as it is usually done with normal relationships likeUser.belongsToMany(User, {through: 'BlockedUsers', as: "Blocks"});User.belongsToMany(User, {through: 'BlockedUsers', as: "Blockeds"});It also does nothing. The DB is created as if there were no relationship.No errors are thrown using any of those ways.​Am I doing something wrong? Do I need to define en empty model named BlockedUsers for that to work?​Note: I am using SQLite for prototyping purposes but it is not supposed to be the culprit here.

Submitted February 22, 2020 at 12:09AM by Kran6a

No comments:

Post a Comment