4
In this relationship, A Questionnaire can have MANY Questions, when I run it in return I don’t get the ID of Questionário
on the table of Questão
, but both are created. Why this occurs?
Here’s 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
});
});