Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

fetching foreign key data in sequelize

Generic :

TableB.belongsTo(models.TableA, { foreignKey: 'someID', targetKey: 'notTableAID' } );

TableA.hasOne(TableB, {
  sourceKey: 'someNonPrimaryKeyColumnOnTheSOurceModel',
  foreignKey: 'matchingColumnOnTheTargetModel'
});



Example : 
// associations
assetAuditSubmissionsModel.hasOne(assetInfoModel, {
  foreignKey: "assetSerialNumber",
  sourceKey: "assetSerialNumber",
});

assetInfoModel.belongsTo(assetAuditSubmissionsModel, {
  foreignKey: "assetSerialNumber",
  targetKey: "assetSerialNumber",
});
 
PREVIOUS NEXT
Tagged: #fetching #foreign #key #data #sequelize
ADD COMMENT
Topic
Name
3+7 =