[docs]deftest_foreignKey():setupClass([Note,PersonWithNotes,Paper,EmployeeWithNotes],force=True)note=Note(text="person")PersonWithNotes(firstName='Oneof',lastName='Authors',note=note)note=Note(text="employee")EmployeeWithNotes(firstName='Project',lastName='Leader',note=note)paper=Paper(content="secret")EmployeeWithNotes(firstName='Senior',lastName='Clerk',paper=paper)PersonWithNotes(firstName='Some',lastName='Person')person=PersonWithNotes.get(1)assertisinstance(person,PersonWithNotes)and \
notisinstance(person,EmployeeWithNotes)assertperson.note.text=="person"employee=EmployeeWithNotes.get(2)assertisinstance(employee,EmployeeWithNotes)assertemployee.note.text=="employee"save_employee=employee# comparison to None needed to build the right SQL expressionpersons=PersonWithNotes.select(PersonWithNotes.q.noteID!=None)# noqaassertpersons.count()==2persons=PersonWithNotes.selectBy(noteID=person.note.id)assertpersons.count()==1# comparison to None needed to build the right SQL expressionemployee=EmployeeWithNotes.select(PersonWithNotes.q.noteID!=None)# noqaassertemployee.count()==1persons=PersonWithNotes.selectBy(noteID=person.note.id)assertpersons.count()==1persons=PersonWithNotes.selectBy(note=person.note)assertpersons.count()==1persons=PersonWithNotes.selectBy(note=None)assertpersons.count()==2employee=EmployeeWithNotes.selectBy(paperID=None)assertemployee.count()==1employee=EmployeeWithNotes.selectBy(paper=None)assertemployee.count()==1employee=EmployeeWithNotes.selectBy(note=save_employee.note,paper=save_employee.paper)assertemployee.count()==1employee=EmployeeWithNotes.selectBy()assertemployee.count()==2