gpt4 book ai didi

node.js - SequelizeEagerLoadingError : model is not associated to otherModel

转载 作者:行者123 更新时间:2023-12-03 12:18:01 27 4
gpt4 key购买 nike

我在不同的论坛上经常看到这个问题,但我一直无法解决它。我在 Sequelize 中使用 Express 和 PostgreSQL。

基本上我有两种模式:校园和学术经理。两者之间是一对一的关系,Campus 有一名学术经理。

我已经实现了两个模型和关联工作,我可以看到它显示在 postgres 服务器的 mi 数据库中。但是当我尝试实现我的校园请求时,我尝试包含经理信息,但没有成功。我不断收到同样的错误:

SequelizeEagerLoadingError: academic_manager is not associated to campus!



我相信我的问题与包含有关

这是我的代码片段:

校园.js
const campus = (sequelize, DataTypes) =>{
const Campus = sequelize.define('campus', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
manager: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'academic_manager',
key: 'id'
}
}
});

Campus.associate = models => {
Campus.hasOne(models.Manager, { foreignKey: 'id'});
}

return Campus;
};

export default campus;


Academic_manager.js
const manager = (sequelize, DataTypes) =>{
const Manager = sequelize.define('academic_manager', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false
}
});

Manager.associate = models => {
Manager.belongsTo(models.Campus, { foreignKey: 'id'});
}

return Manager;
};

export default manager;


和 get 方法:
router.get('/', async (req, res) => {
const campus = await req.context.models.Campus.findAll({
include: [
{ model: req.context.models.Manager }
]
});
return res.send(campus);
});

get 请求在没有包含的情况下工作。我知道关联已经实现,因为当我描述我的校园表和我的经理表时,我有这个:
TABLE "campus" CONSTRAINT "campus_manager_fkey" FOREIGN KEY (manager) REFERENCES academic_manager(id)

最佳答案

您的模型定义有几个问题。 Sequelize 可能会对您是否想要 感到困惑同事 模型,或者只是拥有它们 引用 彼此。

您添加了实现关系约束的关联,但还添加了引用键,这意味着允许引用但不实现约束。

可能想阅读:

  • How to implement many to many association in sequelize
  • https://sequelize.org/v5/manual/associations.html

  • 我已经评论了您的代码:

    校园.js

    const Campus = (sequelize, DataTypes) =>{
    const Campus = sequelize.define('Campus', {
    name: {
    type: DataTypes.STRING,
    allowNull: false,
    }
    // this is not correct.
    // read when to use reference key further below
    /* manager: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
    model: 'academic_manager',
    key: 'id'
    }

    } */
    });

    Campus.associate = models => {
    // Campus.hasOne(models.Manager, { foreignKey: 'id'});
    // foreignKey `id` is wrong. Your models already by default have `id`, which is their own.
    // you either define the name as `campus_id`, or just let sequelize handle it for you by not defining it at all and just use `Campus.hasOne(models.Manager)`.
    // but generally it's good practice to define, otherwise when you need it, you have to figure out what did sequelize help you name it as.
    // read: https://sequelize.org/master/manual/assocs.html#providing-the-foreign-key-name-directly
    Campus.hasOne(models.Manager, { foreignKey: 'campus_id'); // this means please inject `campus_id` into `Manager`
    }

    return Campus;
    };

    export default Campus;

    Academic_manager.js

    const Manager = (sequelize, DataTypes) =>{
    const Manager = sequelize.define('Manager', {
    name: {
    type: DataTypes.STRING,
    allowNull: false,
    },
    email: {
    type: DataTypes.STRING,
    allowNull: false
    }
    });

    Manager.associate = models => {
    // you don't define foreignKey here. because by doing so you are saying
    // you want to inject this foreignKey into `Campus`, which you are not.
    Manager.belongsTo(models.Campus);
    }

    return Manager;
    };

    export default Manager;

    附加信息 - 何时使用此 referenceKey?

    在关系数据库中,经验法则是在任何 2 个模型之间,您应该只有 1 个路径,否则会形成 cyclic dependency ,这可能会导致问题。

    假设您有第三个实体 Location
    Campus.hasOne(Manager)
    Manager.belongsTo(Campus)

    Location.hasOne(Campus)
    Location.belongsTo(Campus)

    一切都很好。让我们想象一下 Manager留在 Campus ,你可以找到他们的 Location像这样
    Manager -> Campus -> Location

    在此之前,您已确定 Manager 是真实的陈述。将永远留在那个 Location没有其他地方,并且您的应用程序更频繁地需要此信息,您会很想这样做:
    Location.hasOne(Manager)
    Manager.belongsTo(Location)

    您现在有了另一条路径 Location -> Manager ,你完成了 Location -> Manager -> Campus -> Location -> Manager....的循环引用

    要打破这一点,您必须在哪种关系更重要或更常访问之间做出选择。在这种情况下, Location 之间的关系似乎是和 Manager不是那么重要,然后继续使用 Manager -> Campus -> Location .

    但你想要蛋糕也吃它

    这是您不关联模型的地方,只需使用 引用键 或使用 constraints: false .阅读 https://sequelize.org/master/manual/constraints-and-circularities.html
    constraints: false是我通常的选择,因为它仍然为我提供了所有的 Sequelize 方法,就好像模型是关联的一样。但要非常严格,您有权使用 reference keys最多。然后进行手动连接,或使用您找到的引用键进行 2 次查询以在其引用表上定位其数据。

    关于node.js - SequelizeEagerLoadingError : model is not associated to otherModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61845632/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com