-
Notifications
You must be signed in to change notification settings - Fork 274
Description
DAB allows customers to define a relationship between two entities (source, target):
- When a relationship is defined in the database (via FK constraint), or
- When the relationship is only defined in the config (but no FK constraint exists in the database).
When a relationship is defined in the database
It is pretty easy to determine the order of insertion between the two entities - We just need to utilize the foreign key constraint metadata collected during the startup from the database to determine which are the referencing and the referenced entities.
When the relationship is only defined in the config
But when relationship exists only in the config file, the metadata collected would not be able to help us determine which entity should be considered as the referencing entity and which entity should be considered as the referenced entity. Rather we would need to rely on the request input data for the source and target entities and perform some logic to determine which entity should be considered as the referenced entity and which should be considered as referencing entity. Giving the customers the ability to specify a custom relationship needs DAB to deal with a lot of scenarios which don't make sense like:
- Both source,target entity contain an autogenerated field: In this case, there does not exist a valid order of insertion because no matter which entity we consider as referencing entity, the insertion would fail because we cannot derive the value of an autogenerated column from the insertion in the referenced entity.
- Both source and target entity specify a value for a relationship field: For insertion in a referencing entity, the values for the referencing columns must come from the insertion in the referenced entity. However, since both source/target entities is specifying a value (either in input or an autogenerated value) for at least one relationship column, this will lead to potential conflicts.
- Neither source nor target entity contain non-null values for all the relationship fields: For an entity to be considered as a referencing entity, it must contain the value for all the relationship fields (either the field is autogenerated or has been provided a value in the input), so that insertion can be performed successfully. But when neither of the entities contain the value for all the relationship fields, there is no valid referenced entity that exists.
If all the above cases do not arise, we conclude that:
- Atmost one of the entity contains an autogenerated field,
- Atmost one of the entity contains values for relationship fields
- The entities mentioned in previous two points are same: because had they been different, we would have violated condition 2.
Hence, by satisfying 1,2,3 - we will be able to find an entity which can be considered as the referencing entity. And thus, a valid order of insertion will exist for this relationship defined in the config.