I haven’t found a way to display what I need to.
I basically have 3 tables:
- parcels
- parcelContracts
- contracts (which is an owner)
Technically, the DB allows that :
- A parcel can be owned by several contracts
- A contract can own several parcels
Hence I have the following relationships:
Contracts.belongsToMany(models.parcels, {
through: 'parcelContracts',
foreignKey: 'contract_id',
otherKey: 'parcel_id',
as: 'parcelsThroughParcelContracts',
});
Parcels.belongsToMany(models.contracts, {
through: 'parcelContracts',
foreignKey: 'parcel_id',
otherKey: 'contract_id',
as: 'contractsThroughParcelContracts',
});
My goal : while I am on a specific parcel summary view, I need to display the list of parcels that belongs to this parcel’s contracts. Even if the DB allows a parcel to be owned by several contracts, the should never be the case, any parcel should belong to one contract only.
I built a smart relationship but when it comes to the query, I can’t use the existing relationship above to specify from which parcel i’m coming from.
I’ve been trying to split into 2 relationships the relations above, such as :
Parcels.hasMany(models.parcelContracts, {
foreignKey: {
name: 'parcelIdKey',
field: 'parcel_id',
},
as: 'parcelContract',
});
and
Contracts.hasMany(models.parcelContracts, {
foreignKey: {
name: 'contractIdKey',
field: 'contract_id',
},
as: 'contractParcel',
});
and wrote a query as follows:
const include = [{
model: models.parcelContracts,
as: 'parcelContract',
include: [{
model: models.contracts,
as: 'contract',
include: [{
model: models.parcelContracts,
as: 'contractParcel',
where: { parcel_id: parcelId }
}]
}]
}];
but I get a error message :
SequelizeDatabaseError: missing FROM-clause entry for table "parcelContract"
Can anybody help to figure this out ?