【问题标题】:Unable to establish one-to-one relation with Sequelize无法与 Sequelize 建立一对一关系
【发布时间】:2023-04-11 09:55:02
【问题描述】:

我声明:

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

感谢Jan Aargaard Meier 的有用回答,我能够将内容更改为:

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`;"

}

注意: * 表名是item * 查询包含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'
});

【问题讨论】:

    标签: javascript mysql node.js sequelize.js


    【解决方案1】:
    Item.find({where : {aid: aid}, include: [{ model: Item, as: 'Parent' }])
    

    如果你给关系一个别名,你必须在进行预加载时提供这个别名(就像你在一个项目实例上调用getParent而不是getItem

    这是因为别名(使用as)允许您为同一个模型创建多个关联,因此当您只提供模型时,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])
    

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

    【讨论】:

    • 感谢您的帮助!你能检查我更新的问题陈述吗?我应该提交错误吗?
    • 尝试将foreignKey 也添加到您的 hasMany 调用中
    • 这很好用!除了... 可悲的是,where 限制在整个连接中传播。我可以以某种方式将where 条目限制为某个表或别名吗?例如。如果我有var queryData = {where : {gid: gid, type: ItemType.Problem}, include: cat.component.childRelation};,我只希望where 应用于初始项,而不是子项。
    • 不太清楚你的意思?您能否更新您的问题或在 sequelize 错误跟踪器上发帖? github.com/sequelize/sequelize/issues
    • 我仍在研究与它相关的代码。完成后,我可以进一步测试并通知您!
    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多