Good afternoon guys, I'm new to Node.js and I'm having a problem. In this relationship, a Questionnaire can have MANY Questions, when I execute it in return I do not get the ID of Questionário
in the Questão
table, however the two are created. Why does this occur?
Here is the code I made:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('gerador', 'root', '');
var Questionario = sequelize.define('Questionario',
{
id: {
type: Sequelize.INTEGER, primaryKey: true, defaultValue: null, autoIncrement:true
},
titulo: {
type: Sequelize.STRING,},
situacao: {
type: Sequelize.STRING,},
datai: {
type: Sequelize.DATE,},
dataf: {
type: Sequelize.DATE}
},{tableName: 'Questionarios'});
var Questao = sequelize.define('Questao',{
id:{
type: Sequelize.INTEGER, primaryKey: true, defaultValue: null, autoIncrement:true},
texto:{
type: Sequelize.STRING,
},
ordem:{
type: Sequelize.INTEGER,
},
},{tableName: 'Questaos'});
Questao.belongsTo(Questionario, {foreignKey:'idQuestionario', as: 'Questionario'});
Questionario.hasMany(Questao, {foreignKey:'idQuestionario', as: 'Questao'});
sequelize.sync({ force: true}).then(function() {
Questionario.create({
titulo: 'Universidade Estacio de Sa',
situacao: 'Ativo',
datai: new Date(2005,1,1),
dataf: new Date(2006,1,2)
});
Questao.create({
idQuestionario:Questionario.id,
texto: 'Qual o seu sexo?',
ordem: 1
});
});