0

I'm not understanding why sequelize is giving me this error.

relation "Likes" does not exist

I referenced a similar question, but it didn't provide me with much of an insight:

Sequelize Error: Relation does not exist

and this too:

Sequelize Migration: relation <table> does not exist

My table names matches the reference model names.

I don't think it has anything to do with the models, but everything to do with the migrations file.

This is what I have

Posts migration

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Posts', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      title: {
        type: Sequelize.STRING
      },
      post_content: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      userId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'

        }
      },
      likeId: {
        type: Sequelize.INTEGER,
        onDelete: 'CASCADE',
        references: {
          model: 'Likes',
          key: 'id'
        }
      },
      username: {
        type: Sequelize.STRING
      },
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Posts');
  }
};

Likes migration

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Likes', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      like: {
        type: Sequelize.BOOLEAN
      },
      userId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id',
          as: 'userId'

        }
      },
      postId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Posts',
          key: 'id',
          as: 'postId'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Likes');
  }
};

models/like.js

'use strict';

const Like = (sequelize, DataTypes) => {
    const Likes = sequelize.define('Likes', {
      like:{
        type:DataTypes.BOOLEAN,
        allowNull:true
      }
    }, {});
    Likes.associate = function(models) {
      Likes.belongsTo(models.User, {
        onDelete: "CASCADE",
        foreignKey: {
          foreignKey: 'userId'
        }
      }) 
      Likes.belongsTo(models.Post, {
        onDelete: 'CASCADE',
        foreignKey: 'likeId',
        targetKey: 'id',      
      })  
  }
    return Likes;
};
module.exports = Like;

models/post.js

module.exports = (sequelize, DataTypes) => {
  const Post = sequelize.define('Post', {
    title: DataTypes.STRING,
    post_content: DataTypes.STRING,
    username:  DataTypes.STRING
  }, {});
  Post.associate = function(models) {
    Post.belongsTo(models.User, {  foreignKey: 'userId',  targetKey: 'id' });
    Post.belongsTo(models.Likes, {  foreignKey: 'likeId',  targetKey: 'id' });
  };
  return Post;
};

models/user.js

'use strict';

const User = (sequelize, DataTypes) => {
  const myUser = sequelize.define('User', {
    username: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    resetPasswordToken:DataTypes.STRING,
    resetPasswordExpires: DataTypes.DATE
  }, {});

  myUser.associate = function(models) {

    myUser.hasMany(models.Post, { foreignKey: 'userId', as:'users' });
    myUser.hasMany(models.Likes, { foreignKey: 'userId', as:'likes' });
  };

  return myUser;
};

module.exports = User;
halfer
  • 19,824
  • 17
  • 99
  • 186
randal
  • 1,272
  • 3
  • 23
  • 48

1 Answers1

0

Instead of adding likeId in the migration. I needed to add a new migration like so

sequelize migration:generate --name add_likeId_to_posts

so we have now

'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
     return queryInterface.addColumn(
      'Posts',
      'likeId',
      {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Likes',
          key: 'id'
        }
      }
    )
  },

  down: function (queryInterface, Sequelize) {
     return queryInterface.removeColumn(
     'Posts',
     'likeId'
   )
  }
};

which gives us

voila!

enter image description here

and likeId is Associated on the table

enter image description here

randal
  • 1,272
  • 3
  • 23
  • 48