![]() ![]() We can check this by opening mycollege.db using SQLiteStudio as shown in the screenshots given below − PRIMARY KEY (department_id, employee_id),įOREIGN KEY(department_id) REFERENCES department (id),įOREIGN KEY(employee_id) REFERENCES employee (id) The Python console emits following CREATE TABLE queries − The relationship function’s secondary attribute is assigned a link as its value.Īll these three tables are created when the following statement is executed − Similarly, Employee class has departments attribute related to Department class. The relationship function’s secondary attribute is assigned a link as its value. Here, we have to make a note that Department class has employees attribute related to Employee class. It is linked to link table and contains department_id and employee_id attributes respectively referencing to primary keys of department and employee table. This constitutes many-to-many relationship.ĭefinition of Employee and Department classes mapped to department and employee table is as follows −įrom sqlalchemy import create_engine, ForeignKey, Column, Integer, StringĮngine = create_engine('sqlite:///mycollege.db', echo = True)įrom import declarative_baseĮmployees = relationship('Employee', secondary = 'link')ĭepartments = relationship(Department,secondary='link') Here, we assume that an employee is a part of more than one department, and a department has more than one employee. Moreover, classes mapping to the two tables have an attribute with a collection of objects of other association tables assigned as secondary attribute of relationship() function.įor this purpose, we shall create a SQLite database (mycollege.db) with two tables - department and employee. Many to Many relationship between two tables is achieved by adding an association table such that it has two foreign keys - one from each table’s primary key. ![]()
0 Comments
Leave a Reply. |