Javascript 无法与Sequelize建立一对一关系

Javascript 无法与Sequelize建立一对一关系,javascript,mysql,node.js,sequelize.js,Javascript,Mysql,Node.js,Sequelize.js,我宣布: Item.hasOne(Item, { foreignKey: 'parentAid', as: 'Parent' }) 我问: Item.find({where : {aid: aid}, include: [Item]}).complete(function(err, article) { .. }; 我得到: Error: Item is not associated to Item! 我做错了什么 徖 更新1 多亏了的帮助性回答,我能够将情况更改为: ItemModel.

我宣布:

Item.hasOne(Item, { foreignKey: 'parentAid', as: 'Parent' })
我问:

Item.find({where : {aid: aid}, include: [Item]}).complete(function(err, article) { .. };
我得到:

Error: Item is not associated to Item!
我做错了什么

更新1

多亏了的帮助性回答,我能够将情况更改为:

ItemModel.belongsTo(ItemModel, { foreignKey: 'parentAid', as: 'Parent', foreignKeyConstraint: true });
ItemModel.hasMany(ItemModel, { as: 'Children', constraints: false });

this.articleRelations.push({
    model: ItemModel,
    as: 'Parent'
});

this.articleRelations.push({
    model: ItemModel,
    as: 'Children'
});

// ...
我现在的问题是:

{where : {aid: aid}, include: this.articleRelations}
但我得到了以下错误:

{
code : "ER_BAD_FIELD_ERROR",
errno : 1054,
sqlState : "42S22",
index : 0,
sql : "SELECT `item`.*, `Parent`.`aid` AS `Parent.aid`, `Parent`.`gid` AS `Parent.gid`, `Parent`.`title` AS `Parent.title`, `Parent`.`type` AS `Parent.type`, `Parent`.`parentAid` AS `Parent.parentAid`, `Parent`.`createdAt` AS `Parent.createdAt`, `Parent`.`updatedAt` AS `Parent.updatedAt`, `Parent`.`itemId` AS `Parent.itemId`, `Parent`.`aid` AS `Parent.aid`, `Parent`.`aid` AS `Parent.aid`, `Parent`.`aid` AS `Parent.aid`, `Children`.`aid` AS `Children.aid`, `Children`.`gid` AS `Children.gid`, `Children`.`title` AS `Children.title`, `Children`.`type` AS `Children.type`, `Children`.`parentAid` AS `Children.parentAid`, `Children`.`createdAt` AS `Children.createdAt`, `Children`.`updatedAt` AS `Children.updatedAt`, `Children`.`itemId` AS `Children.itemId`, `Children`.`aid` AS `Children.aid`, `Children`.`aid` AS `Children.aid`, `Children`.`aid` AS `Children.aid` FROM (SELECT `item`.* FROM `item` WHERE `item`.`aid`=2 LIMIT 1) AS `item` LEFT OUTER JOIN `item` AS `Parent` ON `Parent`.`aid` = `item`.`parentAid` LEFT OUTER JOIN `item` AS `Children` ON `item`.`aid` = `Children`.`itemId`;"
}

注: *表名为
*查询包含
itemId
,我没有在任何地方定义它。那好像是个虫子

作为参考,这是我的模型:

ItemModel = sequelize.define('ExerciseItem', {
        aid: {type: Sequelize.INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true},
        gid: {type: Sequelize.INTEGER.UNSIGNED},
        title: Sequelize.STRING(100),
        type: Sequelize.INTEGER.UNSIGNED,
        parentAid: Sequelize.INTEGER.UNSIGNED
    },{
        freezeTableName: true,
        tableName: 'item'
});
如果给关系一个别名,则在执行紧急加载时必须提供该别名(就像在项目实例上调用
getParent
而不是
getItem

这是因为别名(将
用作
)允许您创建多个与同一模型的关联,因此当您仅提供模型时,sequelize无法知道您实际要加载的模型

我们一直在讨论使用关系调用返回值的能力,例如:

var itemParentRelation = Item.hasOne(Item, { foreignKey: 'parentAid', as: 'Parent' })
Item.find({where : {aid: aid}, include: [itemParentRelation])
// or
Item.find({where : {aid: aid}, include: [item.relations.parent])

但是现在你必须使用文章开头提供的代码:)

谢谢你的帮助!你能检查我更新的问题陈述吗?我应该提交一个bug吗?试着在hasMany调用中添加一个
foreignKey
,效果也很好!除了遗憾的是,
where
限制在整个连接中传播。我是否可以将
where
条目限制为某个表或别名?例如,如果我有
var queryData={where:{gid:gid,type:ItemType.Problem},则包括:cat.component.childRelation}
,我只希望
where
应用于初始项,而不是子项。不太清楚您的意思是什么?您是否可以更新您的问题或在sequelize bug tracker上发布的帖子?我仍在编写与之相关的代码。一旦完成,我可以进一步测试并让您知道!
var itemParentRelation = Item.hasOne(Item, { foreignKey: 'parentAid', as: 'Parent' })
Item.find({where : {aid: aid}, include: [itemParentRelation])
// or
Item.find({where : {aid: aid}, include: [item.relations.parent])