目前,我正在为以下顺序模型建模:
const RideModel = sequelize.define(
"rides",
{
uuid: {
type: Sequelize.UUID,
primaryKey: true
},
pickup_time: {
type: Sequelize.DATE
},
fare: {
type: Sequelize.DOUBLE
},
commision_percentage: {
type: Sequelize.DOUBLE
}
},
{
hooks: {
beforeCreate: (ride, Option) => {
console.info("payment type updated hook");
PaymentType.findOne({
where: {
type: Option.paymentType
}
}).then(paymentType => {
console.info("Setting Payment Type");
ride.paymentType = paymentType;
Option.include.push({ as: "paymentType", model: PaymentType });
console.info(ride.paymentType);
});
}
}
}
);
在上面的代码中,我正在寻找要保存在旅程中的paymentType。 根据日志,我可以看到在创建游乐设施之前已调用了该挂钩。甚至在挂钩内,paymentType的值也被设置为顺次。
只有一件事是我看不到要在mysql数据库中的“ rides”表中保存的paymentTypeUuid。你们可以让我知道使用钩子实现此目标的可能解决方案吗?
日志:
Sync All Tables
payment type updated hook
Setting Payment Type
payment_types {
dataValues:
{ uuid: '111',
type: 'CASH',
description: 'CASH',
createdAt: 2019-03-09T08:54:19.000Z,
updatedAt: 2019-03-09T08:54:19.000Z },
_previousDataValues:
{ uuid: '111',
type: 'CASH',
description: 'CASH',
createdAt: 2019-03-09T08:54:19.000Z,
updatedAt: 2019-03-09T08:54:19.000Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: true,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { type: 'CASH' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: { plural: 'payment_types', singular: 'payment_type' },
omitNull: false,
setterMethods: true,
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [MysqlDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: {},
test: [Object] },
hooks: {},
uniqueKeys: { payment_types_type_unique: [Object] } },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [ 'uuid', 'type', 'description', 'createdAt', 'updatedAt' ] },
__eagerlyLoadedAssociations: [],
isNewRecord: false }
Create a new ride