如何通过sequelize中的findAll查询将父模型包含在子模型中

时间:2019-01-16 10:22:05

标签: sequelize.js

我正在尝试在子模型中包括父模型。但是,我遇到了错误500。在下面的示例中,每个Brand包含多个Jobs

模型

Job.belongsTo(models.Brand) 
Brand.hasMany(models.Job)

路线

   models.Job.findAll({
      where: {
          job_archived_status: archived_status  
        },
        order: [ ['updatedAt', 'DESC'] ],
        include: [{
          model: models.Brand,
          where: {
            id: brand_id
          }
        }]
      }).  
      then(function(jobs) {  
          res.status(200).json(jobs);  
      }, function(error) {  
          res.status(500).send(error);  
      }); 

错误

{
    "name": "SequelizeDatabaseError",
    "parent": {
        "name": "error",
        "length": 98,
        "severity": "ERROR",
        "code": "22P02",
        "position": "550",
        "file": "numutils.c",
        "line": "62",
        "routine": "pg_atoi",
        "sql": "SELECT \"Job\".\"id\", \"Job\".\"job_id\", \"Job\".\"job_archived_status\", \"Job\".\"createdAt\", \"Job\".\"updatedAt\", \"Job\".\"BrandId\", \"Brand\".\"id\" AS \"Brand.id\", \"Brand\".\"brand_name\" AS \"Brand.brand_name\", \"Brand\".\"brand_logo_url\" AS \"Brand.brand_logo_url\", \"Brand\".\"brand_archived_status\" AS \"Brand.brand_archived_status\", \"Brand\".\"createdAt\" AS \"Brand.createdAt\", \"Brand\".\"updatedAt\" AS \"Brand.updatedAt\", \"Brand\".\"ManufacturerId\" AS \"Brand.ManufacturerId\" FROM \"Jobs\" AS \"Job\" INNER JOIN \"Brands\" AS \"Brand\" ON \"Job\".\"BrandId\" = \"Brand\".\"id\" AND \"Brand\".\"id\" = 'null' WHERE \"Job\".\"job_archived_status\" = 'false' ORDER BY \"Job\".\"updatedAt\" DESC;"
    },
    "original": {
        "name": "error",
        "length": 98,
        "severity": "ERROR",
        "code": "22P02",
        "position": "550",
        "file": "numutils.c",
        "line": "62",
        "routine": "pg_atoi",
        "sql": "SELECT \"Job\".\"id\", \"Job\".\"job_id\", \"Job\".\"job_archived_status\", \"Job\".\"createdAt\", \"Job\".\"updatedAt\", \"Job\".\"BrandId\", \"Brand\".\"id\" AS \"Brand.id\", \"Brand\".\"brand_name\" AS \"Brand.brand_name\", \"Brand\".\"brand_logo_url\" AS \"Brand.brand_logo_url\", \"Brand\".\"brand_archived_status\" AS \"Brand.brand_archived_status\", \"Brand\".\"createdAt\" AS \"Brand.createdAt\", \"Brand\".\"updatedAt\" AS \"Brand.updatedAt\", \"Brand\".\"ManufacturerId\" AS \"Brand.ManufacturerId\" FROM \"Jobs\" AS \"Job\" INNER JOIN \"Brands\" AS \"Brand\" ON \"Job\".\"BrandId\" = \"Brand\".\"id\" AND \"Brand\".\"id\" = 'null' WHERE \"Job\".\"job_archived_status\" = 'false' ORDER BY \"Job\".\"updatedAt\" DESC;"
    },
    "sql": "SELECT \"Job\".\"id\", \"Job\".\"job_id\", \"Job\".\"job_archived_status\", \"Job\".\"createdAt\", \"Job\".\"updatedAt\", \"Job\".\"BrandId\", \"Brand\".\"id\" AS \"Brand.id\", \"Brand\".\"brand_name\" AS \"Brand.brand_name\", \"Brand\".\"brand_logo_url\" AS \"Brand.brand_logo_url\", \"Brand\".\"brand_archived_status\" AS \"Brand.brand_archived_status\", \"Brand\".\"createdAt\" AS \"Brand.createdAt\", \"Brand\".\"updatedAt\" AS \"Brand.updatedAt\", \"Brand\".\"ManufacturerId\" AS \"Brand.ManufacturerId\" FROM \"Jobs\" AS \"Job\" INNER JOIN \"Brands\" AS \"Brand\" ON \"Job\".\"BrandId\" = \"Brand\".\"id\" AND \"Brand\".\"id\" = 'null' WHERE \"Job\".\"job_archived_status\" = 'false' ORDER BY \"Job\".\"updatedAt\" DESC;"
}

0 个答案:

没有答案