Sequelize创建对象,关联不起作用

时间:2016-01-04 04:23:33

标签: node.js express associations sequelize.js

我正在尝试创建一个带有关联(天气)的记录(位置)(使用外键插入)以及我尝试的每一种方式我在外键字段中以NULL值结束 weatherId

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

我发现了类似的问题,但没有人回答这个问题。

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    |                |
+--------------------+--------------+------+-----+---------+----------------+

第一次失败, 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');

任何人都可以帮助我,花了这么多时间在这上面......

彼得

1 个答案:

答案 0 :(得分:1)

此问题已在https://github.com/sequelize/sequelize/issues/5138

解决

按要求跑了     的console.log(Object.keys(location.rawAttributes));

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

表格中的 weatherId 在模型中是 WeatherId

第一次尝试 - 拿2

weather pd weatherId:weatherId 至 WeatherId:weatherId

它有效。