简体   繁体   English

如何在 Sequelize 中定义 hasOne 关系?

[英]How to define hasOne relationship in Sequelize?

I have a transaction model that hasOne stripePayment .我有一笔transaction model hasOne stripePayment I want to be able to retrieve a transaction with it's associated stripePayment.我希望能够检索与其关联的 stripePayment 的交易。

When I run the following query:当我运行以下查询时:

const data = await models.Transaction.findOne({
  where: { clientId },
  include: [
    {
      model: models.StripePayment,
    }
  ]
});

It is trying to left outer join where Transaction`.`id` = `StripePayment`.`stripePaymentId when it should be the other way around.它试图在Transaction`.`id` = `StripePayment`.`stripePaymentId的地方离开外部连接,而它应该是相反的。 ie Transaction`.`stripePaymentId` = `StripePayment`.`idTransaction`.`stripePaymentId` = `StripePayment`.`id

My tables look something like this我的桌子看起来像这样

transactions交易

=======
id  |  stripePaymentId     
---------------------
1   |  1a
2   |  2b
3   |  3c
4   |  4d

stripePayments条纹支付

=======
id   |  amount     
---------------------
1a   |  100
2b   |  101
3c   |  102
4d   |  103

I then have my models with the associations defined like this:然后,我的模型具有如下定义的关联:

class Transaction extends Model {
  static associate(models) {
    this.hasOne(models.StripePayment, {
      foreignKey: 'id'
    });
  }
}

Transaction.init(
  {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    stripePaymentId: {
      type: DataTypes.UUID,
      allowNull: true,
      foreignKey: true,
      references: {
        model: stripePayment,
        key: 'id',
      },
    }
  },
  {
    sequelize,
    modelName: 'Transaction',
  }
);

and

class StripePayment extends Model {
  static associate(models) {
    this.belongsTo(models.Transaction, {
      foreignKey: 'stripePaymentId'
    });
  }
}

StripePayment.init(
  {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    amount: {
      type: DataTypes.INTEGER,
      allowNull: false,
    }
  },
  {
    sequelize,
    modelName: 'StripePayment',
  }
);

I was under the impression that a one to one relationship should have a foreign key on the origin table.我的印象是一对一的关系应该在源表上有一个外键。

How can I tell sequelize to join on transaction.stripePaymentId === stripePayment.id ?我如何告诉 sequelize 加入transaction.stripePaymentId === stripePayment.id

You should use belongsTo from a model that has foreign key field and hasOne or hasMany for a parent model to which another model has a reference (foreign key).您应该从具有foreign key字段和hasOnehasMany的 model 中使用belongsTo作为父 model ,另一个 model 具有引用(外键)。
Also, you should indicate foreignKey option while defining associations if a foreign key column is named other than <Child table mode>+<id> .此外,如果外键列的名称不是<Child table mode>+<id> ,则应在定义关联时指明foreignKey选项。

class Transaction extends Model {
  static associate(models) {
    this.belongsTo(models.StripePayment, {
      foreignKey: 'stripePaymentId'
    });
  }
}
...
class StripePayment extends Model {
  static associate(models) {
    this.hasOne(models.Transaction, {
      foreignKey: 'stripePaymentId'
    });
  }
}

Please pay attention that foreignKey option values should be the same for pairs belongsTo/hasOne(hasMany) .请注意,对于belongsTo/hasOne(hasMany)对, foreignKey选项值应该相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM