gpt4 book ai didi

node.js - Sequelize - 包含关联问题

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

怎么了?

我有一个 vehicle/announce 路线,可以从应用程序返回所有车辆,我有 汽车 摩托车 农业机械 农具 。我的问题是从农业机械和农具返回数据。

序列化查询

Annoucement.findAll({
include: [{
model: AnnoucementVehicle,
include: [{
model: Itens,
include: [{
model: AgriculturalMachine,
}],
include: [{
model: Vehicle,
include: [{
model: OptionalHasVehicles,
// as: 'Optionals',
include: [{
model: Optional,
}]
}, {
model: VehicleHasFeatures,
include: [{
model: Features,
}],
// as: 'Optionals',
}, {
model: Fipe,
include: [{
model: Fuel,
}]
}]
}],

}, {
model: VehicleImages
}, {
model: Person
}]
}],
order: [
['id', 'DESC']
],
}).then((result) => {
return res.json({ success: true, result: result })
}).catch((err) => {
return res.status(400).json(err)
});

带有公告车的 JSON
{
"success": true,
"result": [
{
"id": 311,
"active": true,
"announcement_vehicle": {
"idAnnouncementVehicles": 311,
"price": null,
"video": null,
"description": null,
"personId": 1,
"itemId": 297,
"payed": false,
"plans_id": null,
"createdAt": "2020-05-20T19:05:18.000Z",
"updatedAt": "2020-05-20T19:05:18.000Z",
"iten": {
"id": 297,
"type": "1",
"vehicle": {
"item_id": 297,
"mileage": null,
"license": null,
"new": 0,
"armoured": 0,
"fuel_id": 1,
"cambium_id": 1,
"color_id": 1,
"door_id": 1,
"fipe_id": 1,
"createdAt": "2020-05-20T19:05:18.000Z",
"updatedAt": "2020-05-20T19:05:18.000Z",
"optional_has_vehicles": [
{
...
}
],
"vehicles_has_features": [
{
...
"feature": {
...
}
},
],
"fipe": {
....
"fuel": {
"id": 2,
"description": "Gasolina"
}
}
}
},
"announcemment_photos": [],
"person": {
...
}
}
},

但是当它是农具或农业机械时,它会将我返回到车辆到达的位置并显示 vehicle: null

我尝试了什么?

我试图在 association: AnnouncementVehicle.associations.iten 下面放一些类似的东西 include: [{ association: AnnouncementVehicle.associations.agricultural_machine }]
返回与我放在上面相同的 JSON

我注意到了什么?

我意识到它总是返回最后一个关联,也许我正在尝试为两个从相同开始的表做两个包含。我把农机的关联放在了车辆的关联之后,返回的是 agricultural_machine: null而不是返回的车辆。

农业机械模型

module.exports = function (sequelize, DataTypes) {
const agricultural_machine = sequelize.define('agricultural_machine', {
agricultural_machine_id: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
references: {
model: 'itens',
key: 'id'
},
references: {
model: 'agricultural_implement',
key: 'agricultural_implement_id'
},
references: {
model: 'tractor',
key: 'tractor_id'
}
},
worked_hours: {
type: DataTypes.STRING(45),
allowNull: true
},
year: {
type: DataTypes.INTEGER,
allowNull: false
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
brand_agricultural_machine_id: {
type: DataTypes.INTEGER(5).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
references: {
model: 'brand_agricultural_machine',
key: 'id'
}
},
model_agricultural_machine_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: 'model_agricultural_machine',
key: 'id'
}
}
}, {
tableName: 'agricultural_machine',
timestamps: false
});

agricultural_machine.associate = (models) => {
agricultural_machine.hasOne(models.tractor, { foreignKey: 'tractor_id' });
agricultural_machine.hasOne(models.agricultural_implement, { foreignKey: 'agricultural_implement_id' });
agricultural_machine.belongsTo(models.itens, { foreignKey: 'agricultural_machine_id' });
agricultural_machine.belongsTo(models.brand_agricultural_machine, { foreignKey: 'brand_agricultural_machine_id' });
agricultural_machine.belongsTo(models.model_agricultural_machine, { foreignKey: 'model_agricultural_machine_id' });
//announcement_vehicles.belongsTo(models.persons,{foreignKey: 'personId'});
//announcement_vehicles.hasMany(models.announcemment_photos,{foreignKey: 'annoucements_id', constraints: false});
};

return agricultural_machine;
};


伊腾模型

module.exports = function (sequelize, DataTypes) {
const itens = sequelize.define('itens', {
id: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
type: {
type: "SET('1','2','3','4')",
allowNull: false,
defaultValue: '1'
}
}, {
tableName: 'itens',
timestamps: false
});
itens.associate = (models) => {
itens.hasOne(models.vehicles, { foreignKey: 'item_id' });
itens.hasOne(models.agricultural_machine, { foreignKey: 'agricultural_machine_id' });
}

return itens
};


公告车辆型号

module.exports = function (sequelize, DataTypes) {
const announcement_vehicles = sequelize.define('announcement_vehicles', {
idAnnouncementVehicles: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
references: {
model: 'annoucements',
key: 'id'
}
},
price: {
type: DataTypes.DECIMAL,
allowNull: true
},
video: {
type: DataTypes.STRING(45),
allowNull: true
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
personId: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: true,
references: {
model: 'persons',
key: 'id'
}
},
itemId: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: true,
references: {
model: 'itens',
key: 'id'
}
},
payed: {
type: DataTypes.BOOLEAN,
allowNull: true
},
plans_id: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 'announcement_vehicles'
});

announcement_vehicles.associate = (models) => {
announcement_vehicles.belongsTo(models.itens, { foreignKey: 'itemId', constraints: false });
announcement_vehicles.belongsTo(models.persons, { foreignKey: 'personId' });
announcement_vehicles.hasMany(models.announcemment_photos, { foreignKey: 'annoucements_id', constraints: false });
}

return announcement_vehicles
};

公告模式

module.exports = function(sequelize, DataTypes) {
var annoucements = sequelize.define('annoucements', {
id: {
type: DataTypes.INTEGER(11).UNSIGNED.ZEROFILL,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
active: {
type: DataTypes.BOOLEAN
}
}, {
tableName: 'annoucements',
timestamps: false
});
annoucements.associate = (models) => {
annoucements.hasOne(models.announcement_vehicles,{foreignKey: 'idAnnouncementVehicles', constraints: false});
}
return annoucements
};


当它是农业机械或农业工具时我收到的 JSON
 {
"id": 306,
"active": true,
"announcement_vehicle": {
"idAnnouncementVehicles": 306,
"price": "500.000,00",
"video": null,
"description": "TESTE TIPO 4",
"personId": 1,
"itemId": 292,
"payed": false,
"plans_id": null,
"createdAt": "2020-05-18T23:57:18.000Z",
"updatedAt": "2020-05-18T23:57:18.000Z",
"iten": {
"id": 292,
"type": "4",
"vehicle": null
},
"announcemment_photos": [],
"person": {
"id": 1,
"email": "teste@gmail.com",
"password": "$2b$10$3OZVFYWeXKWLfxBMWJBYqua0qJPQkLl3sVgIFvZFuo6qBKj62dMQS",
"name": "teste",
"type": "fis",
"createdAt": "2020-05-14T00:12:59.000Z",
"updatedAt": "2020-05-14T00:12:59.000Z"
}
}
},

提到模型的图表

enter image description here

最佳答案

所有 包括 应如下所示:


include: [{
model: AnnoucementVehicle,
as: 'Alias' // optional if you didn't indicate this alias in an association definition

如果 Itens 包括 ,它必须是:
model: Itens,
include: [{
model: AgriculturalMachine,
}, {
model: Vehicle,
include: [{
... // other includes
}, {
model: AgriculturalImplements,
}, {
model: Vehicle,
include: [{
... // other includes

关于node.js - Sequelize - 包含关联问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61921698/

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