Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Sequelize: Association with alias [alias] does not exist on [model]

i'm using NodeJS & Sequelize for a school project and i'm struggling on making associations w/ sequelize work. I tried a couple of things before but nothing that made my day.

Basically the thing is that a user can have several playlists (hasMany). And a playlist belongs to a user (belongsTo).

My error is: Association with alias "playlist" does not exist on users

Here are my models:

/* USER MODEL */
const Sequelize = require('sequelize');
const { db } = require('../utils/db');

const User = db.define('users', {
  id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER,
  },
  userID: {
    type: Sequelize.INTEGER,
    allowNull: false,
    field: 'user_id',
  },
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name',
    allowNull: false,
  },
}, {
  underscored: true,
  tableName: 'users',
  freezeTableName: true, // Model tableName will be the same as the model name
});

module.exports = {
  User,
};
/* PLAYLIST MODEL */

const sequelize = require('sequelize');
const { db } = require('../utils/db');

const Playlist = db.define('playlist', {
  id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: sequelize.INTEGER,
  },
  name: {
    type: sequelize.STRING,
    field: 'name',
    allowNull: false,
  },
  coverUrl: {
    type: sequelize.STRING,
    field: 'cover_url',
    allowNull: true,
  },
  ownerId: {
    type: sequelize.INTEGER,
    allowNull: false,
    references: {
      model: 'users',
      key: 'user_id',
    },
  },
}, {
  underscored: true,
  tableName: 'playlist',
  freezeTableName: true,
});

module.exports = {
  Playlist,
};

Here is how i load my models:

const { Credentials } = require('./credentials');
const { User } = require('./users');
const { Playlist } = require('./playlist');

function loadModels() {
  User.associate = (models) => {
    User.hasMany(models.Playlist, { as: 'playlist' });
  };

  Playlist.associate = (models) => {
    Playlist.belongsTo(models.User, { foreignKey: 'owner_id', as: 'owner' });
  };

  Credentials.sync({ force: false });
  User.sync({ force: false });
  Playlist.sync({ force: false });
}

module.exports = {
  loadModels,
};

And finally here is my query where i get this error:

const express = require('express');
const { auth } = require('../../middlewares/auth');
const { Playlist } = require('../../models/playlist');
const { User } = require('../../models/users');

const router = express.Router();

router.get('/playlist', [], auth, (req, res) => {
  User.findOne({
    where: { userID: req.user.user_id }, include: 'playlist',
  }).then((r) => {
    console.log(r);
  });
});

module.exports = router;

I'm trying to get all the playlist that belongs to a user.

I removed all the useless code (jwt check etc..) So when i'm doing a get request on /playlist I get: Unhandled rejection Error: Association with alias "playlist" does not exist on users.

I understand the error but don't understand why i get this. What did I miss, any ideas ?

Thanks,

like image 791
Fake Avatar asked Sep 24 '19 19:09

Fake


1 Answers

I finally fixed it by re-make all my models and definitions with migrations. I had the same problem and the solution was that Sequelize pluralize the models name so in my case "playlist" does not exist on users because Sequelize pluralized my model so I had to put "Playlists" instead.

like image 99
Fake Avatar answered Sep 19 '22 15:09

Fake