使用 Sequelize 实现单表继承 [英] Achieve Single Table Inheritance with Sequelize

查看:81
本文介绍了使用 Sequelize 实现单表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用sequelize来创建单表继承?

Is there a way to use sequelize to create single table inheritance?

我想要一个用于 Purchase 和 PartialPurchase 模型的 STI,其中我将有一个类型字段,它可以是Purchase"或PartialPurchase"以及类 Purchase 和 PartialPurchase,它们每个都继承自一个操作类.

I would like to have a STI for a Purchase and PartialPurchase model where I would have a type field which would be "Purchase" or "PartialPurchase" and classes Purchase and PartialPurchase which would each inherit from an Operation class.

我认为 sequelize 不支持此功能,但可以实现吗?

I don't see this as supported by sequelize, but is an implementation possible?

推荐答案

嘿,所以我知道你没有在合理的时间内得到答案,但是你可以在你的数据库中使用一个枚举类型的列,比如你说然后使用 sequelize 的范围功能.Scope 涵盖从查询到模型定义和关联的所有领域.效果很好.我为带有请求和响应的消息表实现了类似的功能.

Hey so I know you didn't get an answer to this, in a reasonable time, but you can use a type column that's an enum in your db like you said and then use sequelize's scope feature. Scope's work in everything from queries to model definitions and associations. It works out pretty well. I implemented something similar for a messages table with requests and responses.

module.exports = function(sequelize, DataTypes) {
  var Message = sequelize.define('Message', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
    },
    type: {
      type: DataTypes.ENUM('REQUEST', 'RESPONSE', 'FILE'),
      allowNull: false
    },
    text: {
      type: DataTypes.TEXT,
      allowNull: false,
    },
    state: {
      type: DataTypes.JSONB,
      allowNull: true,
    },
    userTeamId: {
      type: DataTypes.INTEGER,
      allowNull: false,
    }
  }, );
  Message.associate = (models) => {
    Message.belongsTo(models.UserTeam, { foreignKey: 'userTeamId', targetKey: 'tag'})
  }
  return Message;
};

然后这就是我做关联的方式.

And then this is how I do associations.

module.exports = function(sequelize, DataTypes) {
  var UserTeam = sequelize.define('UserTeam', {
    tag: { type: DataTypes.UUID, allowNull: false, unique: true},
    active: { type: DataTypes.BOOLEAN, defaultValue: false },
    activationNonce: { type: DataTypes.STRING },
    UserId: { type: DataTypes.INTEGER, },
    TeamId: { type: DataTypes.INTEGER, },
    type: DataTypes.ENUM('OWNER', 'ADMIN', 'MEMBER', 'GUEST'),
  },);
  UserTeam.associate = (models) => {
    UserTeam.hasMany(models.Message, { as: 'responses', foreignKey: 'userTeamId', sourceKey: 'tag', scope: {type: 'RESPONSE'}})
    UserTeam.hasMany(models.Message, { as: 'requests', foreignKey: 'userTeamId', sourceKey: 'tag', scope: {type: 'REQUEST'}})
    UserTeam.hasMany(models.Message, { as: 'files', foreignKey: 'userTeamId', sourceKey: 'tag', scope: {type: 'FILE'}})
    UserTeam.hasOne(models.Team, {foreignKey: 'id', sourceKey: 'TeamId'})
    UserTeam.belongsTo(models.User, {foreignKey: 'UserId', sourceKey: 'id'})
  }
  return UserTeam;
};

关联的好处在于,您可以使用 UserTeam 的实例(例如userteam.getFiles()")来获取所有关联文件.

The nice thing about the associations is with that you can do things with an instance of UserTeam like 'userteam.getFiles()' to get all associated files.

这篇关于使用 Sequelize 实现单表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆