- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
怎么了?
我有一个 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)
});
{
"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 }]
。
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
};
{
"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"
}
}
},
最佳答案
所有 包括 应如下所示:
include: [{
model: AnnoucementVehicle,
as: 'Alias' // optional if you didn't indicate this alias in an association definition
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/
我正在关注 Sequelize 纪录片。在 Model Definition 部分的底部,我想知道 sequelize 变量用于什么?我可以删除它吗? { sequelize, modelNa
我有这个原始 SQL 查询。 SELECT restaurants.name AS restaurant, ROUND((6371 * ACOS(COS(RADIANS(6.9271)
我有一个 postgres 数据库,我正在使用 Sequelize。从表 车辆 和 预订 我试图在给定开始日期和结束日期的情况下获得所有可用车辆。 如果我使用给定日期搜索 Bookings: Book
我正在尝试使用 HapiJS 和 Sequelize 开始一个项目,并且开始时,希望在没有 Sequelize CLI 的情况下使事情正常工作,以了解一切是如何结合在一起的。 我见过多个示例项目,例如
sequelize init:models 命令会创建一个“models”文件夹并添加一个 index.js。 有人能解释一下这个 index.js 文件到底做了什么,以及它如何适应 Sequeliz
查看此 Sequelize tutorial 中的 models/blog.js 定义. module.exports = (sequelize, type) => { return sequ
我收到一个错误 No overload matches this call 每当我尝试使用 @HasMany() 或 @BelongsTo 装饰器时。 我正在使用 nestjs/sequelize:
const gamem = await gamemodes.findAll({attributes: ['name']}); const newme = await gamemodes.fin
我们如何使用 sequelize 4.2 创建由所有模型应用/继承的自定义实例方法?在 Sequelize 3 中,我们曾经在扩展到所有其他模型的主模型的“定义”部分中有一个“instanceMeth
我正在使用 Typescript、NodeJS 和sequelize-auto-ts 进行模型生成。我想要放置包含查找查询的两个模型给了我错误的结果,这又是由于触发了错误的查询。这是有问题的两个模型;
假设我在用户表中有 5 列 name email state isOnline createdAt | N
我有两张表,其中一张表具有另一张表的 ID。 1:1 关系。 所以像 EventFeedback somePrimaryKey userEventID UserEvent us
如何使用对象创建具有现有关联的条目? 例如, User.create({ socialMedia: [{ socialId: 1, //should reference existing
如何创建在创建 Sequelize 模型的新实例时以编程方式生成的默认值?我已经在某处阅读了如何执行此操作,但在任何地方都找不到。我以为这与classMethods有关,但我不知道应该调用什么方法。
当我设置关联时,有没有办法使用 Sequelize (sequelizejs.com) 输出所有在对象上神奇创建的函数。 例如;我有一个用户模型,我设置 User.belongsToMany(User
我该如何制作 Music是`音乐? { id: 4 name: "playlist 1" created_at: "2015-04-21T21:43:07.000Z" updated_
我可以使用 sequelize 从可用模型创建数据库模式吗?我在一个缺少许多迁移且没有测试的项目上工作。要运行测试,我需要创建一个新的 db (sqlite3),但我无法使用迁移初始化其架构(因为它们
我有一个与其他模型相关联的简单 Sequelize 模型。 module.exports = function (sequelize, DataTypes) { var Votes = seque
我有一个像这样设置的协会: m.User.hasMany(m.Interests, { joinTableName: 'user_interests', foreignKey: 'user_id' }
我有以下型号: var User = Seq.define('user', { firstName: { type: Sequelize.STRING,
我是一名优秀的程序员,十分优秀!