mercredi 18 janvier 2017

Is ForeignKey concept a violation of SRP?

I'd like to illustrate the general nature of my question with a Django sample:

class Person(models.Model):
    team = models.ForeignKey(Team, related_name='member', on_delete=models.CASCADE)

class Team(models.Model):
    pass

Here, the goal is to create a relationship representing the fact that a single person can be associated with one and only one team.

But, doesn't it violate SRP that Person entity is aware of Team entity, and not vice-versa?

I would rather write something like

class Person(models.Model):
    pass

class Team(models.Model):
    members = models.OneToMany(Person, related_name='team', on_delete=models.CASCADE)

As a consequence, whenever a new Entity-to-Person relationship is introduced (or, an existing one wiped out) there'd be no need to update Person model.

Aucun commentaire:

Enregistrer un commentaire