Node.js Sequelize创建关联不起作用的对象
我试图创建一个带有关联(Weather)的记录(Location)(使用外键插入),每次尝试都会在外键字段weatherId中得到一个空值 我在帮助中看到了同时创建主实体和次实体的示例,但在本例中,我已预加载了天气表,用户仅限于从选择列表中选择项目 我发现了类似的问题,但没有一个能解决这个问题 续集[节点:4.2.2,CLI:2.2.1,ORM:2.0.0-rc1,mysql:^2.10.0] 我的模型是:- 位置 位置的MySQL描述 天气模型 天气预报 第一次尝试在天气ID为空时失败Node.js Sequelize创建关联不起作用的对象,node.js,express,associations,sequelize.js,Node.js,Express,Associations,Sequelize.js,我试图创建一个带有关联(Weather)的记录(Location)(使用外键插入),每次尝试都会在外键字段weatherId中得到一个空值 我在帮助中看到了同时创建主实体和次实体的示例,但在本例中,我已预加载了天气表,用户仅限于从选择列表中选择项目 我发现了类似的问题,但没有一个能解决这个问题 续集[节点:4.2.2,CLI:2.2.1,ORM:2.0.0-rc1,mysql:^2.10.0] 我的模型是:- 位置 位置的MySQL描述 天气模型 天气预报 第一次尝试在天气ID为空时失败 mod
models.Location.create({
locationName: locationName,
weatherId: weatherId
}).then(function(location) {
res.redirect('/location');
}).catch(function(reason) {
console.log(reason);
});
models.Location.create({
locationName: locationName
}).then(function(location) {
models.Weather.find({where: { id: weatherId } }).then(function(weather) {
location.setWeather([weather]).then(function(location) {
res.redirect('/location');
}).catch(function(reason) {
console.log(reason);
});
}).catch(function(reason) {
console.log(reason);
});
}).catch(function(reason) {
console.log(reason);
});
第二次尝试失败,出现NULLweatherId
models.Location.create({
locationName: locationName,
weatherId: weatherId
}).then(function(location) {
res.redirect('/location');
}).catch(function(reason) {
console.log(reason);
});
models.Location.create({
locationName: locationName
}).then(function(location) {
models.Weather.find({where: { id: weatherId } }).then(function(weather) {
location.setWeather([weather]).then(function(location) {
res.redirect('/location');
}).catch(function(reason) {
console.log(reason);
});
}).catch(function(reason) {
console.log(reason);
});
}).catch(function(reason) {
console.log(reason);
});
然而,当我进行更新时,这是有效的:-
models.Location.find({
where: {
id: locationId
}
}).then(function(location) {
if (location) {
location.setWeather([weatherId]).then(function(location) {
location.updateAttributes({
locationName: locationName
}).success(function() {
res.redirect('/location');
});
});
}
}).catch(function(reason) {
console.log(reason);
res.send(reason);
})
日志中没有错误,但weatherId仍然NULL
日志中的SQL不包括天气ID,如下所示:-
INSERT INTO `Location` (`id`,`locationName`,`createdAt`,`updatedAt`) VALUES (NULL,'test me','2016-01-04 02:33:04','2016-01-04 02:33:04');
有人能帮我吗,我在这件事上花了这么多时间
彼得这一问题已在 应要求 log(Object.keys(location.rawtattributes)) 得到 ['id'、'locationName'、'createdAt'、'updatedAt'、'WeatherId'] 因此,表中的weatherId是模型中的weatherId 第一次尝试-采取2 weatherId:weatherId 到 WeatherId:WeatherId 它是有效的
models.Location.find({
where: {
id: locationId
}
}).then(function(location) {
if (location) {
location.setWeather([weatherId]).then(function(location) {
location.updateAttributes({
locationName: locationName
}).success(function() {
res.redirect('/location');
});
});
}
}).catch(function(reason) {
console.log(reason);
res.send(reason);
})
INSERT INTO `Location` (`id`,`locationName`,`createdAt`,`updatedAt`) VALUES (NULL,'test me','2016-01-04 02:33:04','2016-01-04 02:33:04');