0

######## Survey Model ###########

module.exports = function(sequelize, DataTypes) {
const Survey = sequelize.define("Survey", {
    survey_id: {
        autoIncrement: true,
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        primaryKey: true,
    },
    required: {
        type: DataTypes.INTEGER
    },
    title: {
        type: DataTypes.STRING
    },
    description: {
        type: DataTypes.STRING
    },
    status: {
        type: DataTypes.INTEGER
    },
    endDate: {
        type: DataTypes.DATE
    },
    userid: {
        type: DataTypes.INTEGER
    }
},{
    tableName: 'survey',
    hooks,
});
Survey.sync({ alter: true })
Survey.associate = function(models) {
    Survey.belongsTo(models.User, {foreignKey: 'userid'});
    Survey.hasMany(models.Question);
};
return Survey }

############### Question Model ###############

module.exports = function (sequelize, DataTypes) {
const Question = sequelize.define("Question", {
    question_id: {
        autoIncrement: true,
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false,
        primaryKey: true,
    },
    question_type: {
        type: DataTypes.INTEGER
    },
    question_title: {
        type: DataTypes.STRING
    },
    question_required: {
        type: DataTypes.INTEGER
    },
    survey_id: {
        type: DataTypes.INTEGER
    }
}, {
    tableName: 'question',
    hooks,
});
Question.sync({alter: true})
Question.associate = (models) => {
    Question.belongsTo(models.Survey, {foreignKey: 'survey_id'});
    // Question.hasMany(models.QuestionOption, {foreignKey: 'question_id'});
};
return Question }

######### Controller File #########

    Survey.findAll({
    where: {
        userid: userId
    },
    include: {
        model: Question,
    }
}).then(data => {
    res.status(200).send(formatResponse(false, 'Survey list', data));
}).catch(error => {
    res.status(500).send(formatResponse(true, error.message || "Error in Fetching Data", {}));
})

I am beginner in nodejs. I am using postgres for the database. with the help of google and other blog make login , signup api. But when I go for the advance then this error show. Did not understand why this error show. Help me to get out of this. tried with many other solution but the same error come

Avinash
  • 89
  • 9

1 Answers1

0

I think you have to provide a foreign key that will associate Survey with Question. In this case, foreign key will be survey_id.

Survey.hasMany(models.Question, { foreignKey: "survey_id" });

You have to moidify your Survey model like below.

module.exports = function (sequelize, DataTypes) {
const Survey = sequelize.define(
"Survey",
{
  survey_id: {
    autoIncrement: true,
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: false,
    primaryKey: true,
  },
  required: {
    type: DataTypes.INTEGER,
  },
  title: {
    type: DataTypes.STRING,
  },
  description: {
    type: DataTypes.STRING,
  },
  status: {
    type: DataTypes.INTEGER,
  },
  endDate: {
    type: DataTypes.DATE,
  },
  userid: {
    type: DataTypes.INTEGER,
  },
},
{
  tableName: "survey",
  hooks,
}
);
Survey.sync({ alter: true });
Survey.associate = function (models) {
  Survey.belongsTo(models.User, { foreignKey: "userid" });
  Survey.hasMany(models.Question, { foreignKey: "survey_id" });
};
return Survey;
};
Santosh
  • 441
  • 1
  • 4
  • 8