'SQL - How do I create a linkage table?
I'm building the following data model in Django / SQL
Table 1 (Entity) - things that can do things
Entity ID:
Enum: (Person, or Business)
Table 2 (Person)
Entity ID:
Person ID:
Firstname
Lastname
Table 3 (Ltd Co.)
Entity ID:
Ltd Co. ID:
Company Name
Company No.
I'm trying to link a Person, who has a Limited Co., with an Agent (who is also a Person) that also has a Limited Co. So 4 different entitites, two of which are type Person, and two of which are type Limited Co.
Do I need another Linkage Table? E.g.
| Profile Person | Profile Ltd Co | Agent Person | Agent Ltd Co |
|---|---|---|---|
| Entity 1: Type = Person | Entity 3: Type = Business | Entity 2: Type = Person | Entity 4: Type = Business |
Q. How do I create this Linkage Table in a Django Model / or SQL?
Q. Is a Linkage Table the right approach?
UPDATE
Here are snippets from my model.py
class Entity(models.Model):
class EntityTypes(models.TextChoices):
PERSON = 'Person', _('Person')
COMPANY = 'Company', _('Company')
entity_type = models.CharField(
verbose_name='Legal Entity Type',
max_length=7,
choices=EntityTypes.choices,
default=EntityTypes.PERSON,
blank=False,
null=False)
class Person(models.Model):
related_entity = models.OneToOneField(Entity, on_delete=models.CASCADE)
first_name = models.CharField(verbose_name='First Name', max_length=50, blank=True, null=True)
last_name = models.CharField(verbose_name='Last Name', max_length=50, blank=True, null=True)
class LtdCo(models.Model):
related_entity = models.OneToOneField(Entity, on_delete=models.CASCADE)
company_name = models.CharField(verbose_name='Company Name', max_length=50, blank=True, null=True)
company_no = models.CharField(verbose_name='Company No.', max_length=50, blank=True, null=True)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
