gpt4 book ai didi

node.js - 如何使用 sequelize 进行 3 个连接?

转载 作者:行者123 更新时间:2023-12-03 22:36:24 25 4
gpt4 key购买 nike

我正在尝试进行 3 个连接,基本上是 antispam_domain join left domain join left company join left territory
我的代码是:

'use strict';
module.exports = (sequelize, DataTypes) => {

const Domain = sequelize.define('Domain', {});
const Company = sequelize.define('Company', {});
const Territory = sequelize.define('Territory', {});

const Antispam_domain = sequelize.define('Antispam_domain', {
domain_id: DataTypes.INTEGER,
created_by_user_id: DataTypes.INTEGER,
filter_id: DataTypes.INTEGER,
domain: DataTypes.STRING,
status: DataTypes.STRING
}, {});
Antispam_domain.associate = function(models) {
Antispam_domain.belongsTo(models.Domain, {through : 'relation'});
Domain.belongsTo(models.Company, {through : 'relation'});
Company.belongsTo(Territory, {through: 'relation'});
};
return Antispam_domain;
};

`

和`
Antispam_domain
.findAll({
where: {
id: req.body.id
},
include: ['relation']
})
.then((result) => {
if (!result) {
return res.status(401).send({
message: 'Domain not found.',
});
}
res.json({ success: true, result: result});
})
.catch((error) => res.status(400).send(error));

`

但显示响应状态 400 错误请求...

最佳答案

有 :

'use strict';
module.exports = (sequelize, DataTypes) => {
const Antispam_domain = sequelize.define('Antispam_domain', {
domain_id: DataTypes.INTEGER,
created_by_user_id: DataTypes.INTEGER,
filter_id: DataTypes.INTEGER,
domain: DataTypes.STRING,
status: DataTypes.STRING
}, {});
Antispam_domain.associate = function(models) {
Antispam_domain.belongsTo(models.Domain, {foreignKey: 'domain_id'});
models.Domain.belongsTo(models.Company, {foreignKey: 'company_id'});
};
return Antispam_domain;
};

和 :
Antispam_domain
.findAll({
where: {
id: req.body.id
},
include: [{
model : Domain,
include: [{
model: Company
}]
}]
})

关于node.js - 如何使用 sequelize 进行 3 个连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59775537/

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