0

Could anyone help me create this database using the ORM model in Django? I'm stuck at creating a relation between the ATTENDANCE and CLASSDETAIL table. Please look at this ERD:

Entity–relationship model

  • 1
    Welcome to Stack Overflow. Please take the [tour], visit the [help] and read through [ask]. What did you try and what exactly are you stuck on? Show your attempt. – Abdul Aziz Barkat Nov 01 '21 at 03:28
  • I create a ManyToManyField in the CLASS table with through=CLASSDETAIL. But it automatically creates an ID field that I don't need in CLASSDETAIL. I don't know how to create composite primary key in Django Model. But then, I tried to add two ForeignFey in ATTENDANCE that reference to StudentID and ClassID of CLASSDETAIL. This requires me to add UniqueConstraint for both StudentID and ClassID in ATTENDANCE, however it still raises error asking me to add UniqueConstraint or unique=True (unique=True works but that's not my case, I need them unique together, I tried attribute unique_together too) – Bang Nguyen Nov 01 '21 at 08:37
  • @AbdulAzizBarkat thank you for helping me edit the question! – Bang Nguyen Nov 01 '21 at 08:37
  • Unfortunately there are no Composite keys in Django. See [ticket #373](https://code.djangoproject.com/ticket/373). As to the unique together, see [How to define two fields "unique" as couple](https://stackoverflow.com/questions/2201598/how-to-define-two-fields-unique-as-couple). Also any clarification should be edited into the question, not in the comments. – Abdul Aziz Barkat Nov 01 '21 at 10:40
  • thank you so much! – Bang Nguyen Nov 01 '21 at 14:58

1 Answers1

0

You should create a new table(model) named "ClassDetail" with foreign relation with other modeles

AlexZ
  • 113
  • 6
  • That is a ManyToMany relation. Do you know how to create composite key using Django Model? – Bang Nguyen Nov 01 '21 at 08:41
  • Django implement a manytomany relation check this [link](https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/) – AlexZ Nov 01 '21 at 08:50