Sequelize.js Sequelize-我可以基于另一个模型关系关联一个模型吗?

Sequelize.js Sequelize-我可以基于另一个模型关系关联一个模型吗?,sequelize.js,forestadmin,Sequelize.js,Forestadmin,早上好,我一直在尝试解决一个问题,但我没有足够的sequelize经验来知道是否有解决方案,所以我的问题是: 我有一个名为Order的模型,另一个名为Service的模型,还有8个名为“orderAttributeA”、“orderAttributeB”、“orderAttributeC”的模型 我的订单模型如下所示: module.exports = (sequelize, DataTypes) => { const {Sequelize} = sequelize; // Learn m

早上好,我一直在尝试解决一个问题,但我没有足够的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;
};
我的OrderAttributexx型号之一:

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;
};
我试图做的是选择相应的模型(orderAttributeXXX)基于我的服务的类名,所以关系如下:Order.serviceId->service,其中id=Order.serviceId->get the service className列->根据service className列查找模型,但是模型“service”和任何“orderAttributeXXX”模型之间没有链接。有可能在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现成支持的内容(动态参考字段)

针对这类问题的黑客解决方案是A+

例如,您可以这样定义一个智能集合
OrderAttributes

collection('OrderAttributes', {
  fields: [{ 
    field: 'id', // This is mandatory
    type: 'String' 
  }, { 
    field: 'orderAttributeA', 
    type: 'String', 
  }, { 
    field: 'orderAttributeA', 
    type: 'orderAttributeB',
  }, ...]
});

然后覆盖
/forest/order属性
/forest/order
等的路由,以处理
订单
&
订单属性

之间的路由是否相关?是的,很抱歉,如果这是一个重复的主题位。我不知道我们在森林社区网站上已经有帖子了。我明白了!谢谢,那我就那样做