Express - Sequelize - 在查询

时间:2015-10-17 23:35:29

标签: mysql node.js express sequelize.js

我能够对images表格进行查询并正确提取记录,但我希望修改我的查询以包含来自我连接的外表的body字段通过description_id到我的主要表格。我在我的数据库中创建了这种关系,但不确定我的续集模型代码是否需要更改,或者我的查询的简单更改是否会实现我正在寻找的内容。

这是我的问题:

router.get('/:pattern/:color/result', function(req, res, image){

    console.log(req.params.color);
    console.log(req.params.pattern);

    Images.findAll({ 
        where: {
            pattern: req.params.pattern,
            color: req.params.color
        },
        attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id']
    }).then(function(image){
        console.log(image.description_id);
        //console.log(doc.descriptions_id);
        res.render('pages/result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
            })
        });
});

这是我的images模型:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize('db', 'admin', 'pwd', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});


var Images = sequelize.define('images', {
    pattern: {
        type: Sequelize.STRING,
        field: 'pattern'
    },
    color: {
        type: Sequelize.STRING,
        field: 'color'
    },
    imageUrl: {
        type: Sequelize.STRING,
        field: 'imageUrl'
    },
    imageSource: {
        type: Sequelize.STRING,
        field: 'imageSource'
    },
    description_id: {
        type: Sequelize.INTEGER,
        field: 'description_id'
    }
});

module.exports = Images;

description型号:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('db', 'admin', 'pwd', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql'
});

var Description = sequelize.define('description', {
    color: {
        type: Sequelize.STRING,
        field: 'color'
    },
    body: {
        type: Sequelize.STRING,
        field: 'body'
    }
});

module.exports = Description;

2 个答案:

答案 0 :(得分:0)

您需要为模型创建一些关联(请参阅docs for defining associations)。然后,在您的查询中,您需要添加include选项(请参阅docs about querying associations)。

答案 1 :(得分:0)

您应该使用associations

e.g。一对一协会

var Images = sequelize.define('images', {
    //properties..
    description_id: {
        type: Sequelize.INTEGER,
        field: 'description_id'
    }
},{
  classMethods: {
    associate: function(models) {
      Image.belongsTo(models.Description, {foreignKey: 'description_id'});
    }
});

现在您可以通过getter Image#getDescription()获取说明。

 var Description = require('description.js') //require description model
 Images.findAll({ 
            where: {
                pattern: req.params.pattern,
                color: req.params.color
            },
            include: [ Description ], /*make join with associated model's table*/
            attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id']
  }).then(function(image){
     console.log(image.getDescription());
     //console.log(doc.descriptions_id);
     res.render('pages/result.hbs', {
       pattern : req.params.pattern,
       color : req.params.color,
       image : image
     })
  });