Fix SQL bugs in inheritance