Wednesday, October 8, 2014

Multi-table inheritance models not working for me

Hello,

I want to make a hierarchy over a DB design as described in "Fundamentals of database systems" from Elmasri & Navathe. 

This implies that when I have some info which is shared for many classes/tables, I can put it in a main parent table and use the main table id as foreign key in the child tables, kind of a weak entity.

I tried using abstract and multitable inheritance (this last one doesn't let me specify the OneToOneField, don't know where to find this at django docs).

My example is right down here (one table per class):

    '''I would like this to be abstract, because I will never instantiate it, 
    but could be not if needed'''

    class Person(models.Model): 
        personId = models.IntegerField(primary_key=True)
        name = models.CharField(max_length=45)
        surname = models.CharField(max_length=45, blank=True)
        email = models.CharField(max_length=45, blank=True)
        phone = models.CharField(max_length=15, blank=True)

        class Meta:
            managed = False
            db_table = 'person'

    class Alumn(Person):
        # Maybe this one down should be OneToOne.
        # alumnId == personId always true for the same real world guy
        alumnId = models.ForeignKey('Person', db_column='alumnId', primary_key=True) 
                                                                                                                                                           
        comments = models.CharField(max_length=255, blank=True)

    class Meta:
        managed = False
        db_table = 'alumn'
        
    # There are more child classes (Client, Professor, etc....) 
    # but for the example this is enough

My target is achieving to create an Alumn in DB just with two sentences like:


    a = Alumn(personId=1,name='Joe', [...more params...] , alumnId=1, comments='Some comments' )
    a.save()


and having these two lines insert two rows: one for Person and one for Alumn. The alumnId attribute in this snippet up here could be omitted, because it will always be the same as the personId (I told you, like a weak entity).

I'm quite a beginner at django but I have looked at the documentation and proved some things with abstract=True in Person and not having succeeded I guess now that I should mess with the __init__ constructors for getting the superclass built and after that build the child class.

I don't know the right path to choose but definitely want not to alter the database design. Please help.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e25813f2-6205-4457-83fe-8fadffddda69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment