Inheritance in the bulb / SqlAlchemy

I have a class structure that is similar to the one below. The table created by sqlalchemy with db.create_all looks good. Jobs are added with the corresponding columns filled in (school_id for Teachers, precinct_id for police officers). My problem occurs when trying to call do_stuff ():

p = Teacher(...) p.do_stuff() 

returns "hey im the parent", which is the return value of Job. Therefore, despite the fact that everything is correctly inserted into the database, it seems that the actual inheritance does not occur, and the only model that is in place is the task model. Is there a way to fix this or should I install it differently?

 class Job(db.Model): id = Column(Integer, primary_key=True) ... def __init__(...): ... def do_stuff(self): print 'hey im the parent' class Teacher(Job): school_id = Column(Integer, ForeignKey('school.id')) def __init__(...): super(Teacher, self).__init__(...) self.school_id = school_id def do_stuff(self): print 'teacher here' class Policeman(Job): precinct_id = Column(Integer, ForeignKey'precinct.id')) def __init__(...): super(Policeman, self).__init__(...) self.precinct_id = precinct_id def do_stuff(self): print 'police ack' 
+6
source share
1 answer

I believe that you need to specify the relationship between the models using the backlink or db.relationship() . These answers already offer several options: backref and db.relationship and foreign key relationships

0
source

Source: https://habr.com/ru/post/947142/


All Articles