Reputation: 51
class Employee(Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
name = Column(String(50))
type = Column(String(50))
__mapper_args__ = {
'polymorphic_identity':'employee',
'polymorphic_on':type
}
class Engineer(Employee):
__tablename__ = 'engineer'
id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
engineer_name = Column(String(30))
__mapper_args__ = {
'polymorphic_identity':'engineer',
}
e = Engineer()
session.add(e)
session.commit()
Creating an object of Engineer will add a row in both employee and engineer table.
session.delete(e)
session.commit()
Now when I delete the engineer object I want to only remove the row in engineer table and not in the employee table. How do I achieve this? Right now the rows in both table gets removed.
Any help/suggestions would be highly appreciated.
Upvotes: 5
Views: 3387
Reputation: 28253
If you delete the engineer object using the ORM (by calling session.delete(obj)
), then it will delete records from both tables.
However, what you want may be accomplished by
session.query(Engineer).filter(Engineer.id==e.id).delete()
session.commit()
Upvotes: 6