Sequelize - 我可以根据另一个模型关系关联一个模型吗?
Sequelize - Can i associate a model based on another model relation?
早上好,我一直在努力解决一个问题,但我对 sequelize 没有足够的经验,不知道是否有解决方案,所以这是我的问题:
我有一个名为 Order 的模型,另一个名为 Service 的模型和另外 8 个名为“orderAttributeA”、“orderAttributeB”、“orderAttributeC”的模型....
我的订单模型如下:
module.exports = (sequelize, DataTypes) => {
const {Sequelize} = sequelize;
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const Order = sequelize.define('order', {
id: {
type: DataTypes.BIGINT,
autoIncrement: true,
primaryKey: true
},
userId: {
type: DataTypes.BIGINT,
allowNull: false,
},
companyId: {
type: DataTypes.BIGINT,
allowNull: false,
},
serviceId: {
type: DataTypes.BIGINT,
allowNull: false,
},
providerId: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: null
},
status: {
type: DataTypes.INTEGER,
allowNull: false,
},
userComment: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null
},
amount: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
},
deliveryDate: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'Order',
});
Order.associate = async (models) => {
Order.belongsTo(models.user, {
foreignKey: 'userId',
});
Order.hasMany(models.orderDetail, {
foreignKey: 'orderId',
as: 'details',
});
Order.belongsTo(models.service, {
foreignKey: 'serviceId',
as: 'service',
});
Order.hasOne(models.orderPayment, {
foreignKey: 'orderId',
as: 'payment',
});
};
return Order;
};
我的服务模式:
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Service = sequelize.define('service', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
className: {
type: DataTypes.STRING,
allowNull: false,
},
categoryId: {
type: DataTypes.INTEGER,
allowNull: false,
},
externalLink: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'Service',
});
Service.associate = (models) => {
};
return Service;
};
我的一个 orderAttributeXXX 型号:
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const OrderAttributeB = sequelize.define('orderAttributeB', {
orderId: {
type: DataTypes.BIGINT,
primaryKey: true,
allowNull: false,
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'OrderAttributeB',
});
OrderAttributeB.associate = (models) => {
};
return OrderAttributeB;
};
我想做的是 select 基于我服务的类名的相应模型 (orderAttributeXXX),因此关系如下所示:Order.serviceId -> 服务所在的位置id = order.serviceId -> 获取服务类名列 -> 根据服务类名列查找模型 但是模型“服务”和任何“orderAttributeXXX”模型之间没有 link。是否有可能在 sequelize 中实现类似的东西?:
// i would like to select the corresponding model (orderAttribute) based on the column className of my model service
Order.belongsTo(models.service.className, {
foreignKey: 'orderId',
as: 'attribute',
});
提前致谢
这不是 forestadmin 开箱即用支持的东西(动态参考字段)。
针对此类问题的一个 hacky 解决方案是 smart collection + smart field
例如,您可以定义一个智能集合 OrderAttributes
定义如下
collection('OrderAttributes', {
fields: [{
field: 'id', // This is mandatory
type: 'String'
}, {
field: 'orderAttributeA',
type: 'String',
}, {
field: 'orderAttributeA',
type: 'orderAttributeB',
}, ...]
});
然后覆盖 /forest/order-attributes
、/forest/order
等的路由以处理 Order
和 OrderAttributes
之间的 smart relationship。
早上好,我一直在努力解决一个问题,但我对 sequelize 没有足够的经验,不知道是否有解决方案,所以这是我的问题:
我有一个名为 Order 的模型,另一个名为 Service 的模型和另外 8 个名为“orderAttributeA”、“orderAttributeB”、“orderAttributeC”的模型....
我的订单模型如下:
module.exports = (sequelize, DataTypes) => {
const {Sequelize} = sequelize;
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const Order = sequelize.define('order', {
id: {
type: DataTypes.BIGINT,
autoIncrement: true,
primaryKey: true
},
userId: {
type: DataTypes.BIGINT,
allowNull: false,
},
companyId: {
type: DataTypes.BIGINT,
allowNull: false,
},
serviceId: {
type: DataTypes.BIGINT,
allowNull: false,
},
providerId: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: null
},
status: {
type: DataTypes.INTEGER,
allowNull: false,
},
userComment: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null
},
amount: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false,
},
deliveryDate: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: null
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'Order',
});
Order.associate = async (models) => {
Order.belongsTo(models.user, {
foreignKey: 'userId',
});
Order.hasMany(models.orderDetail, {
foreignKey: 'orderId',
as: 'details',
});
Order.belongsTo(models.service, {
foreignKey: 'serviceId',
as: 'service',
});
Order.hasOne(models.orderPayment, {
foreignKey: 'orderId',
as: 'payment',
});
};
return Order;
};
我的服务模式:
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Service = sequelize.define('service', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
className: {
type: DataTypes.STRING,
allowNull: false,
},
categoryId: {
type: DataTypes.INTEGER,
allowNull: false,
},
externalLink: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'Service',
});
Service.associate = (models) => {
};
return Service;
};
我的一个 orderAttributeXXX 型号:
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const OrderAttributeB = sequelize.define('orderAttributeB', {
orderId: {
type: DataTypes.BIGINT,
primaryKey: true,
allowNull: false,
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP')
},
}, {
tableName: 'OrderAttributeB',
});
OrderAttributeB.associate = (models) => {
};
return OrderAttributeB;
};
我想做的是 select 基于我服务的类名的相应模型 (orderAttributeXXX),因此关系如下所示:Order.serviceId -> 服务所在的位置id = order.serviceId -> 获取服务类名列 -> 根据服务类名列查找模型 但是模型“服务”和任何“orderAttributeXXX”模型之间没有 link。是否有可能在 sequelize 中实现类似的东西?:
// i would like to select the corresponding model (orderAttribute) based on the column className of my model service
Order.belongsTo(models.service.className, {
foreignKey: 'orderId',
as: 'attribute',
});
提前致谢
这不是 forestadmin 开箱即用支持的东西(动态参考字段)。
针对此类问题的一个 hacky 解决方案是 smart collection + smart field
例如,您可以定义一个智能集合 OrderAttributes
定义如下
collection('OrderAttributes', {
fields: [{
field: 'id', // This is mandatory
type: 'String'
}, {
field: 'orderAttributeA',
type: 'String',
}, {
field: 'orderAttributeA',
type: 'orderAttributeB',
}, ...]
});
然后覆盖 /forest/order-attributes
、/forest/order
等的路由以处理 Order
和 OrderAttributes
之间的 smart relationship。