【问题标题】:Sequelize create object with association not workingSequelize 创建关联不工作的对象
【发布时间】:2016-04-07 16:22:08
【问题描述】:

我正在尝试使用关联(天气)(使用外键插入)创建一条记录(位置),并且我尝试的每一种方式最终都会在外键字段中得到一个 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');

谁能帮我解决这个问题,花了这么多时间..

彼得

【问题讨论】:

    标签: node.js express associations sequelize.js


    【解决方案1】:

    这个问题已经在https://github.com/sequelize/sequelize/issues/5138解决了

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

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

    所以表中的 weatherId 是模型中的 WeatherId

    第一次尝试 - 参加 2

    weatherId:weatherId 到 WeatherId:weatherId

    它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2014-01-05
      相关资源
      最近更新 更多