3

I am trying to create a user > friend relationship in sequelize. Therefore, I created a user model and a belongsToMany relationship through a join-table called friends. It created the table but the foreignKeys userId and friendId are not created. I would be happy if someone could help me out.

User model:

var User = sequelize.define('user', {
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    emailaddress: {
      type: Sequelize.STRING
    },
    firstname: {
      type: Sequelize.STRING
    },
    lastname: {
      type: Sequelize.STRING
    },
    description: {
      type: Sequelize.STRING
    },
    password: {
      type: Sequelize.STRING
    }
  }, {
    freezeTableName: true, 
    classMethods: {
      associate: function (models) {
        User.belongsToMany(models.user, {
          as: "user",
          through: "friend"
        });
        User.belongsToMany(models.user, {
          as: "friend",
          through: "friend"
        });
        User.sync({
            force: true
          })
      }
    }
  });

friend model

var Friend = sequelize.define('friend', {
    // userId: DataTypes.INTEGER,
    // friendId: DataTypes.INTEGER,
    status: {
      type: DataTypes.BOOLEAN,
      defaultValue: 0
    }
  }, {
    freezeTableName: true,
    classMethods: {
      associate: function (models) {
        Friend.sync()
      }
    }
  });

This generates the following field in the friend table:

id status createdAt updatedAt

I would like the following fields:

id status userId friendId createdAt updatedAt

package.json > "sequelize": "^3.17.1",

0

2 Answers 2

2
+100

You can set otherKey and foreignKey to a sequelize associations. I think you should try this:

User.belongsToMany(models.user, {
    as: "friends",
    through: "friend",
    otherKey: 'userId',
    foreignKey: 'friendId'
});

Sorry I can't try it right now, but I hope, it helps you.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it out and I came to the conclusion that it did not solve my problem, although it is a nice way to achieve the same result. My problem was the fact that I call User.sync directly when initializing every table, when I removed the sync from every table and only did one sync, using the sequelize express example code (I am using express) it worked.
I would still like to assign to you the 100 bounty because your answer pointed me in the right direction. update: will be dealt with in 2 hours. I am not allowed to award it yet ;)
2

After some debugging I found out that the problem was that I called Table.sync on initializing every table instead of doing it just once when all tables are defined (see the example code in the sequelize express example on github: https://github.com/sequelize/express-example). This corrupted the table creation process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.