【问题标题】:Node js and Sequelize insert to databaseNode js和Sequelize插入数据库
【发布时间】:2018-08-18 10:23:55
【问题描述】:

在我的项目中,我使用 MySQL 数据库和 Sequelize Js, 我创建了两个模型:

邮编型号:

module.exports = function(sequelize, Sequelize) {

var Post_code = sequelize.define('post_code', {

    id: {
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER(11)
    },

    code: {
        type: Sequelize.STRING(16),
        allowNull: false,
        unique: true
    },

    city: {
        type: Sequelize.STRING(45),
        allowNull: false
    }
},{
    freezeTableName: true,
    underscored: true
});

Post_code.associate = function(models) {
    models.post_code.hasMany(models.building);
};

   return Post_code;
}

建筑模型:

module.exports = function(sequelize, Sequelize) {

var Building = sequelize.define('building', {

    id: {
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
        type: Sequelize.INTEGER(11)
    },

    name: {
        type: Sequelize.STRING(100),
        allowNull: false
    },

    number: {
        type: Sequelize.INTEGER(11),
        allowNull: false
    },

    address: {
        type: Sequelize.STRING(255),
        allowNull: false
    },

    latitude: {
        type: Sequelize.DECIMAL(9,6),
        allowNull: false
    },

    longitude: {
        type: Sequelize.DECIMAL(9,6),
        allowNull: false
    }
},{
    freezeTableName: true,
    underscored: true
});

Building.associate = function (models) {
    models.building.belongsTo(models.post_code, {
        foreignKey: {
            allowNull: false
        }
    });

    models.building.hasMany(models.flat);
};

    return Building;
};

它们是一对多的,因此邮政编码有许多建筑物:

当 POST 请求发送到此路由时,我想向数据库添加新建筑物:

"/post-codes/:post_code_id/buildings"

我可以访问 post_code_id,但我不知道如何正确地将 post_code 模型与建筑相关联。

我试图做这样的事情:

models.post_code.findById(req.params.post_code_id)
.then(function(postCode){
    postCode.setBuilding(
        models.building.create({
        }).then(function(building){});  
    );    
});

但没有结果。如果有人能解释如何正确插入,我将不胜感激。

【问题讨论】:

  • 您不应该只需要从收到的参数中添加将 Post-codeId 添加到对象的建筑物吗?
  • @Ellebkey 是的,没错,但我正在寻找一些在 Sequelize 中建立关联的内置方法。另一件事是,是的,我可以将 post_code_id 直接添加到我要创建的建筑物中,但是如果发生这种情况,该参数将是数据库中不存在的 post_code 的“id”,我会收到错误消息。我没有找到简单的解释,但 model.create 中有“包含”选项,我可以使用它以更安全的方式创建关联。

标签: node.js express model insert sequelize.js


【解决方案1】:

我还没有听说过 create 的 include 选项,但你可以这样做:

db.post_code.find({
  where: {
    id: req.params.post_code_id
  }
}).then(post_code => {
  if (!post_code) {
    return res.status(404).send({
      message: 'No post_code with that identifier has been found'
    });
  } else {
    //create here your building
  }
}).catch(err => {
  return res.jsonp(err)
});

我不知道这是否是最好的方法,但它首先验证 post_code 是否存在。

【讨论】:

  • link doc 这是有关此选项的文档的链接,如果您愿意,可以查看一下。无论如何,谢谢您的回复。
  • 这就是定义 N:M 关系的方式,在您的情况下,您只需要 1:N 关系。 include 在您需要加入时使用它。因此,当您在 Building 上使用 find 时,您将包含 de post_code,因为它属于
  • 但如果您要查看“创建“HasMany”或“BelongsToMany”关联的元素”部分,我的案例考虑“BelongsToMany”,因为建筑属于许多邮政编码,它们在那里呈现使用许多标签创建/关联产品的方法,接下来,他们将标签:(键)设置为标签/对象数组
猜你喜欢
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2016-09-29
  • 1970-01-01
  • 2018-06-01
相关资源
最近更新 更多