Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
How to save entry in Sequelize with hasOne relationship?

I have a model Transaction with a hasMany association to StripePayment.

I want to create a new Transaction with a related StripePayment like so:

await models.Transaction.create({
  amount,
  stripePayment: {
    stripeId: paymentIntent.id,
    status: stripePaymentStatuses[status],
    clientSecret: paymentIntent.client_secret,
  }
});

This creates a new Transaction but does not create the stripePayment.

My model is as such:

// transaction model
class Transaction extends Model {
    
  static associate(models) {
    this.hasOne(models.StripePayment, { 
      as: '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',
      },
    },
    amount: {
      type: DataTypes.INTEGER,
      allowNull: false,
    }
  },
  {
    sequelize,
    modelName: 'Transaction',
  }
);
// stripePayment
class StripePayment extends Model {
    
  static associate(models) {
    this.belongsTo(models.Transaction, { 
      as: 'Transaction', 
      foreignKey: 'stripePaymentId' 
    });
  }
}

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

I feel like the model is not aware of stripePayment when I pass it into the create method. Am I structuring this correctly?

How can I create a Transaction with a related StripePayment?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You just need to indicate the include option while calling the create method, see creating with associations:

await models.Transaction.create({
  amount,
  // pay attention: this prop should be named exactly as an alias of a model and because it's `hasMany` you should wrap it into an array
  StripePayment: [{
    stripeId: paymentIntent.id,
    status: stripePaymentStatuses[status],
    clientSecret: paymentIntent.client_secret,
  }],
  include: [{
    model: StripePayment,  
    as: 'StripePayment',
  }]
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda