【发布时间】:2018-08-29 23:04:30
【问题描述】:
我正在查询特定的“学生”,包括公司的多对多关系,但受骗者没有返回具有相同 studentId 和 companyId 的记录。连接表也有一个带有 id 的主键。
看看我包含 Companies 的那一行,它是一个 belongsToMany 关系
独特,约束并没有做太多尝试,但偏执狂起作用了。那时我能够获取 deletedAt 记录。
export function getStudent(req, res, next) {
Students.find({
where: {
id: req.params.id
},
order: [
[{model: Addresses}, 'id', 'DESC'],
[{model: Companies}, 'id', 'DESC'],
],
include: [{model: Schools},
{model: ClassificationTypes},
{model: Companies,
as: 'Companies',
paranoid: false,
through: { paranoid: false, unique: false, constraints: false }
},
{model: Drivers,
as: 'Drivers'},
{model: Addresses,
as: 'Addresses',
through: { paranoid: false },
include: [{model: AddressTypes},
{model: States,
paranoid: false }]
}
]
}).then((students) => {
return res.json(students);
}).catch((err) => {
res.sendStatus(500);
next(err);
});
}
Here are my models:
export default (sequelize, DataTypes) => {
const CompaniesStudents = sequelize.define('CompaniesStudents', {
companyId: DataTypes.INTEGER,
studentId: DataTypes.INTEGER,
extraFees: DataTypes.FLOAT,
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
}
}, {
timestamps: true,
paranoid: true,
}
);
return CompaniesStudents;
};
```
```js
export default (sequelize, DataTypes) => {
const Companies = sequelize.define('Companies', {
name: DataTypes.STRING,
description: DataTypes.STRING,
phone: DataTypes.STRING,
email: DataTypes.STRING,
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
}
}, {
timestamps: true,
paranoid: true,
}
);
Companies.associate = (models) => {
Companies.belongsToMany(models.Drivers, {through: 'CompaniesDrivers', as: 'Drivers', foreignKey: 'companyId', otherKey: 'driverId'});
Companies.belongsToMany(models.Students, {through: 'CompaniesStudents', as: 'Students', foreignKey: 'companyId', otherKey: 'studentId'});
Companies.belongsToMany(models.Zones, {through: 'CompaniesZones', as: 'Zones', foreignKey: 'companyId', otherKey: 'zoneId'});
Companies.belongsToMany(models.User, {through: 'CompaniesUsers', as: 'Users', foreignKey: 'companyId', otherKey: 'userId'});
Companies.belongsToMany(models.Addresses, {through: 'CompaniesAddresses', as: 'Addresses', foreignKey: 'companyId', otherKey: 'addressId'});
};
return Companies;
};
export default (sequelize, DataTypes) => {
const Students = sequelize.define('Students', {
studentIdentification: DataTypes.INTEGER,
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
schoolId: {
type: DataTypes.INTEGER,
references: {
model: 'Schools',
key: 'id'
}
},
classificationTypeId: {
type: DataTypes.INTEGER,
references: {
model: 'ClassificationTypes',
key: 'id',
}
},
zoneId: {
type: DataTypes.INTEGER,
references: {
model: 'Zones',
key: 'id'
}
},
desiredPickUpTime: DataTypes.DATE,
desiredDropOffTime: DataTypes.DATE,
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
}
}, {
timestamps: true,
paranoid: true,
}
);
Students.associate = (models) => {
Students.belongsTo(models.ClassificationTypes, { foreignKey: 'classificationTypeId'});
Students.belongsTo(models.Schools, {foreignKey: 'schoolId'});
Students.belongsTo(models.Zones, {foreignKey: 'zoneId'});
Students.belongsToMany(models.Companies, {through: 'CompaniesStudents', as: 'Companies', foreignKey: 'studentId', otherKey: 'companyId'});
Students.belongsToMany(models.Drivers, {through: 'DriversStudents', as: 'Drivers', foreignKey: 'studentId', otherKey: 'driverId'});
Students.belongsToMany(models.Addresses, {through: 'StudentsAddresses', as: 'Addresses', foreignKey: 'studentId', otherKey: 'addressId'});
};
return Students;
};
我希望所有记录都能返回,无论它们是否被软删除。
只返回与之相关的最后一条记录
这是一个学生所属的两家不同公司的示例 以下是记录:
"Companies": [
{
"id": 19,
"name": "Suspect Icecream truck",
"description": "",
"phone": "21312314",
"email": "koawewa",
"createdAt": "2018-03-20T19:41:03.578Z",
"updatedAt": "2018-03-20T19:41:16.500Z",
"deletedAt": null,
"CompaniesStudents": {
"companyId": 19,
"studentId": 6,
"extraFees": null,
"createdAt": "2018-03-20T20:05:03.239Z",
"updatedAt": "2018-03-20T20:05:03.239Z",
"deletedAt": null
}
},
{
"id": 18,
"name": "Karans Pickup Company",
"description": "",
"phone": "917321312",
"email": "a21@yasd.com",
"createdAt": "2018-03-20T19:40:39.012Z",
"updatedAt": "2018-03-20T19:40:54.042Z",
"deletedAt": null,
"CompaniesStudents": {
"companyId": 18,
"studentId": 6,
"extraFees": null,
"createdAt": "2018-03-20T19:42:50.399Z",
"updatedAt": "2018-03-20T19:42:50.399Z",
"deletedAt": "2018-03-20T19:43:02.784Z"
}
}
],
【问题讨论】:
标签: javascript mysql node.js postgresql sequelize.js