gpt4 book ai didi

node.js - Sequelize 创建关联的对象不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:17 25 4
gpt4 key购买 nike

我正在尝试创建一条具有关联(天气)的记录(位置)(使用外键插入),并且我尝试的每一种方式最终都会在外键字段中得到 NULL 值 weatherId.

我在帮助中看到了同时创建主要和次要实体的示例,但在这种情况下,我已经预加载了天气表,并且用户只能从选择列表中选择一个项目。

我发现了类似的问题,但没有一个能解决问题。

Sequelize [ Node :4.2.2,CLI:2.2.1,ORM:2.0.0-rc1,mysql:^2.10.0]

我的模型是:-

位置

'use strict';
module.exports = function(sequelize, DataTypes) {
var Location = sequelize.define('Location', {
id: DataTypes.INTEGER,
locationName: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
Location.hasMany(models.Rig);
Location.belongsTo(models.Weather);
}
},
freezeTableName: true,
tableName: 'Location',
timestamps: true
});
return Location;
};

MySQL 描述位置

mysql> desc Location;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| locationName | varchar(255) | YES | | NULL | |
| weatherId | int(11) | YES | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+

天气模型

'use strict';
module.exports = function(sequelize, DataTypes) {
var Weather = sequelize.define('Weather', {
id: DataTypes.INTEGER,
weatherDescription: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
Weather.hasMany(models.Location);
}
},
freezeTableName: true,
tableName: 'Weather',
timestamps: true
});
return Weather;
};

MySQL 天气描述

mysql> desc Weather;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| weatherDescription | varchar(255) | YES | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+

第一次尝试失败,并显示 NULL weatherId

models.Location.create({
locationName: locationName,
weatherId: weatherId

}).then(function(location) {
res.redirect('/location');

}).catch(function(reason) {
console.log(reason);
});

第二次尝试失败,并显示 NULL weatherId

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 不包含 weatherId,如下所示:-

INSERT INTO `Location` (`id`,`locationName`,`createdAt`,`updatedAt`) VALUES (NULL,'test me','2016-01-04 02:33:04','2016-01-04 02:33:04');

谁能帮我解决这个问题,我花了很多时间在这上面..

彼得

最佳答案

此问题已在 https://github.com/sequelize/sequelize/issues/5138 处得到解决

按照要求运行 console.log(Object.keys(location.rawAttributes));

并且得到了 [ 'id', 'locationName', 'createdAt', 'updatedAt', 'WeatherId' ]

因此表中的weatherId就是模型中的WeatherId

第一次尝试 - 进行 2

天气ID:天气ID到天气ID:天气ID

并且它有效。

关于node.js - Sequelize 创建关联的对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com