gpt4 book ai didi

mysql - Sequelize 中未处理的拒绝 SequelizeEagerLoadingError - Nodejs

转载 作者:行者123 更新时间:2023-11-29 15:53:02 28 4
gpt4 key购买 nike

这是我的父模型Providers

    module.exports = function(sequelize, DataTypes) {
const Providers = sequelize.define('Providers', {
provider_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_name: DataTypes.STRING,
address: DataTypes.STRING,
city: DataTypes.STRING,
state: DataTypes.STRING,
zip: DataTypes.STRING,
phone: DataTypes.STRING,
website: DataTypes.STRING,
accepting_new_clients: DataTypes.INTEGER
});

Providers.associate = models => {
Providers.belongsTo(model.Providerclients, {foreignKey: "provider_id" })
}
return Providers;
}

这是我的子模型Providerclients

    module.exports = function(sequelize, DataTypes) {
const Providerclients = sequelize.define('provider_clients', {
provider_client_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_id: DataTypes.INTEGER,
client_id: DataTypes.INTEGER
},{});

Providerclients.associate = (models) => {
Providerclients.belongsToMany(models.Providers, {foreignKey: "provider_id"});
};


return Providerclients;
}

这是我的另一个子模型Providerinsurance

    module.exports = function(sequelize, DataTypes) {
const Providerinsurance = sequelize.define('provider_insurance', {
provider_insurance_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_id: DataTypes.INTEGER,
insurance_id: DataTypes.INTEGER
},{});

return Providerinsurance;
}

我正在尝试从加入提供商客户端表的提供商表中获取数据。需要明确的是,我想以 mysql 格式运行以下查询

 select * from providers as p left join provider_clients as pc on p.provider_id = pc.provider_id left join provider_insurance as pi on p.provider_id = pi.provider_id

我尝试加入 Sequelize,正如您在 Providers 和 Providerclients 模型中看到的那样。我收到以下错误

Unhandled rejection SequelizeEagerLoadingError: provider_clients is not associated to Providers!

最佳答案

这是您的结构中的关联问题。将您的关联更改为 hasMany

provider.hasMany(clientProvider)

这会将提供商 ID 添加到客户端提供商中。

http://docs.sequelizejs.com/manual/associations.html#one-to-many-associations--hasmany-

这是一个总结

Player.belongsTo(Team)  // `teamId` will be added on Player / Source model

User.belongsTo(Company, {foreignKey: 'fk_company'}); // Adds fk_company to User

User.belongsTo(Company, {foreignKey: 'fk_companyname', targetKey: 'name'}); // Adds fk_companyname to User
========================================================================
Coach.hasOne(Team) // `coachId` will be added on Team / Target model

============================================================================
Project.hasMany(User)
This will add the attribute projectId or project_id to User. Instances of Project will get the accessors getWorkers and setWorkers.

Project.hasOne(User)
hasOne will add an attribute projectId to the User model!

关于mysql - Sequelize 中未处理的拒绝 SequelizeEagerLoadingError - Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668290/

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