Home > Blockchain >  Interconnected Schema
Interconnected Schema

Time:01-31

I have a Schema for employee database, But there are two tables which are inter connected to each other, How can I create model for that?

Please find schema image here

CodePudding user response:

First you use lazy reference in your models. Doing so prevents issue during migrations. Next you use related_name in order to avoid clashes between the department field in Employee and the manager field in Department.

class Employee(models.Model):
    ssn = models.CharField(max_length=20, unique=True)
    department = models.ForeignKey('Department', on_delete=models.CASCADE)

class Department(models.Model):
    manager = models.ForeignKey('Employee', on_delete=models.CASCADE, related_name='manager')

I think you can figure out how to put the other fields

  •  Tags:  
  • Related