我试图返回所有活动的学生地址和软删除的地址
以下是涉及的三个模型:
```js
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', foreignKey: 'studentId', otherKey: 'companyId'});
Students.belongsToMany(models.Drivers, {through: 'DriversStudents', foreignKey: 'studentId', otherKey: 'driverId'});
Students.belongsToMany(models.Addresses, {through: 'StudentsAddresses', as: 'Addresses', foreignKey: 'studentId', otherKey: 'addressId'});
};
return Students;
};
```
```js
export default (sequelize, DataTypes) => {
const Addresses = sequelize.define('Addresses', {
streetName: DataTypes.STRING,
streetNameAlt: DataTypes.STRING,
zipCode: DataTypes.STRING,
city: DataTypes.STRING,
town: DataTypes.STRING,
stateId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'States',
key: 'id'
}
},
addressTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'AddressTypes',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
},
}, {
timestamps: true,
paranoid: true,
}
);
Addresses.associate = (models) => {
Addresses.belongsTo(models.AddressTypes, {foreignKey: 'addressTypeId'});
Addresses.belongsTo(models.States, {foreignKey: 'stateId'});
Addresses.belongsToMany(models.Drivers, {through: 'DriversAddresses', foreignKey: 'addressId', otherKey: 'driverId'});
Addresses.belongsToMany(models.Schools, {through: 'SchoolsAddresses', foreignKey: 'addressId', otherKey: 'schoolId'});
Addresses.belongsToMany(models.Companies, {through: 'CompaniesAddresses', foreignKey: 'addressId', otherKey: 'companyId'});
Addresses.belongsToMany(models.User, {through: 'UsersAddresses', foreignKey: 'addressId', otherKey: 'userId'});
Addresses.belongsToMany(models.Students, {through: 'StudentsAddresses', as: 'Students', foreignKey: 'addressId', otherKey: 'studentId'});
};
return Addresses;
};
```
```js
export default (sequelize, DataTypes) => {
const StudentsAddresses = sequelize.define('StudentsAddresses', {
studentId: DataTypes.INTEGER,
addressId: DataTypes.INTEGER,
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
deletedAt: {
type: DataTypes.DATE
}
}, {
paranoid: true,
timestamps: true,
}
);
return StudentsAddresses;
};
```
这是执行actionables的api端点:
```js
export function getStudent(req, res, next) {
Students.find({
where: {
id: req.params.id
},
paranoid: false,
include: [{model: Schools},
{model: ClassificationTypes},
{model: Addresses,
paranoid: false,
as: 'Addresses',
include: [{model: AddressTypes,
paranoid: false },
{model: States,
paranoid: false }]
}
]
}).then((students) => {
return res.json(students);
}).catch((err) => {
res.sendStatus(500);
next(err);
});
}
```
我希望所有记录都是软删除或不返回
我只回复活动记录。
```js
import Sequelize from 'sequelize';
import sequelizeConfig from '../sequelize_config';
import { ENV } from '../../../../config/env';
import userModel from './users';
import roleModel from './roles';
import userRoleModel from './userroles';
import stateModel from './states';
import zoneModel from './zones';
import districtModel from './districts';
import holidayModel from './holidays';
import schoolModel from './schools';
import studentModel from './students';
import companyModel from './companies';
import driverModel from './drivers';
import pickupStatusModel from './pickupstatus';
import dropoffStatusModel from './dropoffstatus';
import invoiceStatusModel from './invoicestatus';
import driverStudentActionModel from './driverstudentaction';
import schoolInvoiceModel from './schoolinvoice';
import statusModel from './status';
import schoolMonthlyComplianceModel from './schoolmonthlycompliance';
import privilegeModel from './privileges';
import blockDayModel from './blockdays';
import addressModel from './addresses';
import addressTypeModel from './addresstype';
import classificationTypeModel from './classificationtypes';
import studentsAddressesModel from './studentsaddresses';
const config = sequelizeConfig[ENV];
const db = {};
const dbUrl = process.env[config.use_env_variable];
const sequelize = dbUrl ? new Sequelize(dbUrl) : new Sequelize(config.database, config.username, config.password, config);
db.User = sequelize.import('User', userModel);
db.Roles = sequelize.import('Roles', roleModel);
db.UserRoles = sequelize.import('UserRoles', userRoleModel);
db.States = sequelize.import('States', stateModel);
db.Zones = sequelize.import('Zones', zoneModel);
db.Districts = sequelize.import('Districts', districtModel);
db.Holidays = sequelize.import('Holidays', holidayModel);
db.Schools = sequelize.import('Schools', schoolModel);
db.Students = sequelize.import('Students', studentModel);
db.Companies = sequelize.import('Companies', companyModel);
db.Drivers = sequelize.import('Drivers', driverModel);
db.PickupStatuses = sequelize.import('PickupStatuses', pickupStatusModel);
db.DropoffStatuses = sequelize.import('DropoffStatuses', dropoffStatusModel);
db.InvoiceStatuses = sequelize.import('InvoiceStatuses', invoiceStatusModel);
db.DriverStudentActions = sequelize.import('DriverStudentActions', driverStudentActionModel);
db.SchoolInvoices = sequelize.import('SchoolInvoices', schoolInvoiceModel);
db.Statuses = sequelize.import('Statuses', statusModel);
db.SchoolMonthlyCompliances = sequelize.import('SchoolMonthlyCompliances', schoolMonthlyComplianceModel);
db.Privileges = sequelize.import('Privileges', privilegeModel);
db.BlockDays = sequelize.import('BlockDays', blockDayModel);
db.Addresses = sequelize.import('Addresses', addressModel);
db.AddressTypes = sequelize.import('AddressTypes', addressTypeModel);
db.ClassificationTypes = sequelize.import('ClassificationTypes', classificationTypeModel);
db.StudentsAddresses = sequelize.import('StudentsAddresses', studentsAddressesModel);
Object.keys(db).forEach((key) => {
const model = db[key];
if (model.associate) {
model.associate(db);
}
});
export { db as Models, sequelize };
```
我正在使用postgres 10,最新版本的sequelize。这个问题在早期版本的4.x sequelize中仍然存在。
答案 0 :(得分:0)
对于任何想要解决这个问题的迷失的灵魂,我不知道为什么我无法在文档中找到它(我想它嵌入在那里的某处)但是它会是这样的:
export function getStudent(req, res, next) {
Students.find({
where: {
id: req.params.id
},
paranoid: false,
include: [{model: Schools},
{model: ClassificationTypes},
{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);
});
}
您可以在查询和禁用偏执时使用through选项,以及查询多个表