将关联创build对象关联不起作用
我想创build一个关联(天气)(插入与外键)的logging(位置)和每一个我尝试的方式我最终在外键字段weatherId NULL值。
我已经看到了帮助同时创build主实体和辅助实体的示例,但在这种情况下,我已经预加载了“天气”表,用户仅限于从select列表中select项目。
我发现了类似的问题,但没有人回答这个问题。
Sequelize [节点: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 | | +--------------------+--------------+------+-----+---------+----------------+
首先尝试失败与天气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是Model中的WeatherId 。
第一次尝试 – 采取2
weatherId:weatherId到WeatherId:weatherId
它的工作。