![]() INSERT INTO writer (name) VALUES (?) ('Mayur',) ![]() INSERT INTO writer (name) VALUES (?) ('Payal',) INSERT INTO technology_section (name) VALUES (?) ('Back End',) INSERT INTO technology_section (name) VALUES (?) ('Front End',) INSERT INTO technology_section (name) VALUES (?) ('Database',) When running on the python console, the omitting of all the below queries is done. Now, its time to commit all the changes that we have made in the database tables and classes by simply creating an object of Session that is current_session, as shown here – from SQLAlchemy.orm import sessionmaker We can now write the values of the technology section and writers’ values in each other’s table’s attributes to relate them as shown below, which creates many to many relationships – w1.technology_sections.append(ts1) Ts3 = Technology_sections(name = "Back End") Ts2 = Technology_sections(name = "Front End") ts1 = Technology_sections(name = "Database") Now, we will go for creating three sample objects of technology_sections and writers class. We can even give check on the contents by opening them on the SQLiteStudio inside the educba.db. When run on python console, the omitting of all the create table queries is done. We can go for the creation of all the above classes by executing the following statements – That is class of technology_sections contains the writer’s attribute while the writer’s class contains the technology_sections attribute, which is related to the opposite classes. The secondary attribute of the relationship function is given to the value of relationship_link. It’s time to write the definition of Relationship_link class which will be Relationship_linked to the table relationship_link containing technology_section_id and writer_id attributes, which refer to technology_section and writer table’s primary keys, respectively. Technology_sections = relationship(Technology_sections,secondary='relationship_link') Writers = relationship('Writers', secondary = 'relationship_link') ![]() ![]() The classes of Technology_sections and the Writers have the following definition, which is further mapped to the corresponding tables, namely technology_sections and writers – from SQLAlchemy import create_engine, ForeignKey, Column, Integer, StringĮngine = create_engine('sqlite:///educba.db', echo = True)įrom import declarative_base Here, the tables technology_sections and the writers have many relationships. However, a single writer can work for multiple technology sections in the organization, which is nothing but refer to as many to many relationships. To understand SQLAlchemy’s many relationships, let us consider one example where we will create a small database of SQLite named educba, which contains two tables, namely technology_sections and writers, where a general assumption is made that a single technology_sections may have multiple writers working on it. All the steps will be defined in detail with the help of an example where we will define the source classes and the association table for the technology sections and the writers. There are many real-time scenarios where we will need this relationship to be implemented in SQL Alchemy.
0 Comments
Leave a Reply. |