无法包含模型来检索值

时间:2017-02-10 11:05:44

标签: mysql node.js express sequelize.js

这是我的模型对象和get方法。

模型对象:models.js

var Client = sequelize.define("Client", {
    name: DataTypes.STRING(100),
    status : DataTypes.STRING(1)
});

var Lob = sequelize.define("Lob", {
    name : DataTypes.STRING(100),
    status : DataTypes.STRING(1)
});

Lob.belongsTo(Client, { constraints: true, foreignKeyConstraint:true  });

获取方法:

router.get('/getlobs', function(req, res) {
    models.Lob.find({ where: { "id": 2000 }, include: [models.Client], attributes:["id", "name", "status"]}).then(function(lobs) {
        return res.send(lobs);
    });
});

使用out包含它可以正常获取所需的属性,而我正在尝试获取包含客户端模型的错误。实际上我打算使用外键关系获取ClientId的客户端名称。

查询

Executing (default):
SELECT `Lob`.`id`, `Lob`.`name`, `Lob`.`status`, `Client`.`id`
  AS `Client.id`, `Client`.`Client_Name`
  AS `Client.Client_Name`, `Client`.`Address`
  AS `Client.Address`, `Client`.`Status`
  AS `Client.Status`, `Client`.`Created_By`
  AS `Client.Created_By`, `Client`.`createdAt`
  AS `Client.createdAt`, `Client`.`updatedAt`
  AS `Client.updatedAt` FROM `Lobs`
  AS `Lob` LEFT OUTER JOIN `Clients`
  AS `Client` ON `Lob`.`ClientId` = `Client`.`id` WHERE `Lob`.`id` = 2000;

错误

Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'Client.Client_Name' in 'field list'
at Query.formatError (/home/mobi/QA/qa/node_modules/sequelize/lib/dialects/mysql/query.js:175:14)
at Query._callback (/home/mobi/QA/qa/node_modules/sequelize/lib/dialects/mysql/query.js:49:21)
at Query.Sequence.end (/home/mobi/QA/qa/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
at Query.ErrorPacket (/home/mobi/QA/qa/node_modules/mysql/lib/protocol/sequences/Query.js:88:8)
at Protocol._parsePacket (/home/mobi/QA/qa/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/home/mobi/QA/qa/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/home/mobi/QA/qa/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/mobi/QA/qa/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
at TCP.onread (net.js:536:20)

0 个答案:

没有答案