DaronSchmit / DinnerAndAMovie

A fullstack project brought to you by my teammates and I
MIT License
0 stars 2 forks source link

UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: Dinner is not associated to Dinner #48

Closed Kay0s closed 3 years ago

Kay0s commented 3 years ago

After adding to the routes/index.js router.use("/api/movie", require("./apiRoutes/movie-routes")); to test in Postman the routes, image

the below error message shows in the VSC terminal after executing the GET : http://localhost:8080/api/dinner/All in Postman image

Kay0s commented 3 years ago

models/dinner.js

"use strict"; const { Model } = require("sequelize"); module.exports = (sequelize, DataTypes) => { class Dinner extends Model { /**

Kay0s commented 3 years ago

models/dinner.js changed foreignKey : { allowNull: false} to foreignKey: "id" and made http://localhost:8080/api/dinner/All in Postman. Still received 404 but no unhandled promise error in VSC terminal. image image

Kay0s commented 3 years ago

https://stackoverflow.com/questions/50841912/sequelize-js-is-not-associated-to

Asked 2 years, 7 months ago Active 2 years, 7 months ago Viewed 11k times

Report this ad

5

I have some issue with getting full data from db. That are my models:

User

module.exports = function(sequelize, DataTypes) { return sequelize.define('user', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true, field: 'ID' }, password: { type: DataTypes.STRING(255), allowNull: false, field: 'password' }, email: { type: DataTypes.STRING(255), allowNull: false, unique: true, field: 'email' }, roleId: { type: DataTypes.INTEGER(11), allowNull: false, references: { model: 'role', key: 'ID' }, field: 'role_id' } }, { timestamps: false, tableName: 'user' }); }; Role

module.exports = function(sequelize, DataTypes) { return sequelize.define('role', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true, field: 'ID' }, name: { type: DataTypes.STRING(255), allowNull: false, unique: true, field: 'name' }, description: { type: DataTypes.STRING(255), allowNull: false, field: 'description' }, permission: { type: DataTypes.INTEGER(11), allowNull: false, field: 'permission' } }, { timestamps: false, tableName: 'role', });}; I want to get object of one specific user including all role content. Somethink like

{ id: 4, password: 'xxx', email: 'adsads@saas.com', role: { id: 2, name: 'admin' description: 'ipsum ssaffa', permission: 30 } } So I'm using:

User.findOne( { where: { id: req.userId }, include: [ Role ] } ).then( user =>{...}); but I get in the result err.message: "role is not associated to user"

And the simple question - what's wrong ? :)

*to handle models I'm using sequelize-cli

javascript node.js database sequelize.js Share Edit Follow asked Jun 13 '18 at 16:17

The4ECH 7711 gold badge11 silver badge44 bronze badges add a comment 1 Answer

6

You get this error because you didn't add associate between the models

base on your json I see that each user only has one role, so you can either use belongsTo in role model or hasOne in user model

Should be something like this:

User.js

module.exports = function(sequelize, DataTypes) { var user = sequelize.define('user', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true, field: 'ID' }, password: { type: DataTypes.STRING(255), allowNull: false, field: 'password' }, email: { type: DataTypes.STRING(255), allowNull: false, unique: true, field: 'email' }, roleId: { type: DataTypes.INTEGER(11), allowNull: false, references: { model: 'role', key: 'ID' }, field: 'role_id' } }, { timestamps: false, tableName: 'user' }); user.associate = function(models) { user.hasOne(models.role, {foreignKey: 'id',sourceKey: 'roleId'});

}
return user;

}; Role.js

module.exports = function(sequelize, DataTypes) { var role = sequelize.define('role', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true, field: 'ID' }, name: { type: DataTypes.STRING(255), allowNull: false, unique: true, field: 'name' }, description: { type: DataTypes.STRING(255), allowNull: false, field: 'description' }, permission: { type: DataTypes.INTEGER(11), allowNull: false, field: 'permission' } }, { timestamps: false, tableName: 'role', }); role.associate = function(models) { user.belongsTo(models.role, {foreignKey: 'id'});

}
return role;

}; Share Edit Follow edited Jun 13 '18 at 17:19 answered Jun 13 '18 at 17:11

feiiiiii 1,17911 gold badge88 silver badges2020 bronze badges 3 hmmmm, ok - so "references" key in user.roleId isn't enought ? – The4ECH Jun 14 '18 at 11:18 I don't think so, atleast associate is what i've been using to link models together for the latest version of sequelize. – feiiiiii Jun 14 '18 at 13:09 1 Thanks! It works but you make little mistake :) Should be: In role: role.associate = ( models ) =>{ role.hasMany( models.user, { foreignKey: 'roleId', sourceKey: 'id' } ); }; In user: user.associate = ( models ) =>{ user.belongsTo( models.role, { foreignKey: roleId, targetKey: id } ); }; – The4ECH Jul 3 '18 at 12:35 Hi @The4ECH. I tried following your comment ^ and feiii's answer, but I really can't make mine work. We have the very same structure of user and rols. Could you please share your working setup? – Glenn Posadas May 10 '20 at 19:12

Kay0s commented 3 years ago

models/movie.js

'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Movie extends Model { /**

added foreignKey: "id" to Movie.hasMany(models.Dinner, {onDelete: "cascade", foreignKey: "id"}

"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Movie extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      Movie.hasMany(models.Dinner, {
        foreignKey: "id",
        onDelete: "cascade",
      });
    }
  }
  Movie.init(
    {
      title: DataTypes.STRING,
      createdAt: {
        type: DataTypes.DATE,
        defaultValue: sequelize.literal("NOW()"),
      },
      updatedAt: {
        type: DataTypes.DATE,
        defaultValue: sequelize.literal("NOW()"),
      },
    },
    {
      sequelize,
      modelName: "Movie",
    }
  );
  return Movie;
};

image

"use strict"; const { Model } = require("sequelize"); module.exports = (sequelize, DataTypes) => { class Dinner extends Model { /**

image

made http://localhost:8080/api/dinner/All in Postman. Still received 404 image