首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sequelize -我可以基于另一个模型关系关联一个模型吗?

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

Stack Overflow用户
提问于 2021-06-07 21:44:22
回答 1查看 46关注 0票数 0

早上好,我一直在尝试解决一个问题,但我没有足够的经验来知道是否有解决方案,所以这是我的问题:

我有一个名为Order的模型,另一个名为Service的模型和其他8个名为"orderAttributeA","orderAttributeB","orderAttributeC“的模型。

我的订单模型的工作原理如下:

代码语言:javascript
复制
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;

};

我的服务模型:

代码语言:javascript
复制
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模型:

代码语言:javascript
复制
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;
};

我尝试做的是基于我的服务的className选择相应的模型(orderAttributeXXX),因此关系看起来像这样: Order.serviceId ->服务,其中id = order.serviceId ->获取服务className列->根据服务className列查找模型,但在模型“Order.serviceId”和任何"orderAttributeXXX“模型之间没有链接。在sequelize中有可能实现这样的事情吗?

代码语言:javascript
复制
// 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',
    });

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-08 21:08:03

这不是forestadmin开箱即用支持的东西(动态引用字段)。

这类问题的一个很好的解决方案是使用smart collection + smart field

例如,您可以定义一个如下定义的智能集合OrderAttributes

代码语言:javascript
复制
collection('OrderAttributes', {
  fields: [{ 
    field: 'id', // This is mandatory
    type: 'String' 
  }, { 
    field: 'orderAttributeA', 
    type: 'String', 
  }, { 
    field: 'orderAttributeA', 
    type: 'orderAttributeB',
  }, ...]
});

然后覆盖/forest/order-attributes/forest/order等的路由,以处理OrderOrderAttributes之间的smart relationship

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67872830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档