In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
"pointing"
That is, to define the data entries in one table to point to the entries in another table, and to establish this kind of directed "pointing" allows the table to query the pointed entries in the form of fields, so if you want a two-way query, you need to point in both directions.
One To Many
Add the id of the "one" party to the "multiple" square table as a ForeignKey constraint, and both parties need to define the relationship () field for query convenience.
Class Parent (Base): _ _ tablename__ = 'parent'id = Column (Integer, primary_key=True) children = relationship ("Child", back_populates= "parent") class Child (Base): _ _ tablename__ =' child'id = Column (Integer, primary_key=True) parent_id = Column (Integer, ForeignKey ('parent.id') parent = relationship ("Parent", back_populates= "children")
One To One
Not much different from One To Many, as long as the "one" party's relationship method adds a "uselist=False" parameter, uselist is a scalar attribute (a scalar attribute), which means that the "one" party does not use a list for entries in another table.
Class Parent (Base): _ _ tablename__ = 'parent'id = Column (Integer, primary_key=True) child = relationship ("Child", uselist=False, back_populates= "parent") class Child (Base): _ tablename__ =' child'id = Column (Integer, primary_key=True) parent_id = Column (Integer, ForeignKey ('parent.id')) # this field parent = relationship ("parent", back_populates= "child") # usually uses backref as a function to represent the uselist=False parameter Used to show that. # parent = rlationship ("parent", backref=backref ("child", uselist=False))
Many To Many
With the help of intermediate tables, using the secondary parameters supported by relationship
Association_table = Table ('association', Base.metadata, Column (' left_id', Integer, ForeignKey ('left.id')), Column (' right_id', Integer, ForeignKey ('right.id')) class Parent (Base): _ _ tablename__ =' left' id = Column (Integer, primary_key=True) children = relationship ("Child", secondary=association_table) Back_populates= "parents") class Child (Base): _ _ tablename__ = 'right' id = Column (Integer, primary_key=True) parents = relationship ("Parent", secondary=association_table, back_populates= "children")
Note that secondary can accept'a callable that returns the ultimate argument,which is evaluated only when mappers are first used. 'that is, it accepts executable parameters, allowing association_table to be defined later, or even after all modules have been initialized, until it is callable.
Class parent (Base): _ _ tablename__ = "left" id = Column (Integer, primary_key=True) children = relationship ("Child", secondary=lambda: assciation_table, backref= "parents")
The above relationship parameters can be replaced by a class name string.
Delete Mang To Many records, you don't have to delete the intermediate table data of secondary manually, the database will be deleted automatically according to the "cascade rule" cascading rule.
If the intermediate table object needs to be called
Class Association (Base): _ _ tablename__ = 'association' left_id = Column (Integer, ForeignKey (' left.id'), primary_key=True) right_id = Column (Integer, ForeignKey ('right.id'), primary_key=True) extra_data = Column (String (50)) child = relationship ("Child", back_populates= "parents") parent = relationship ("Parent") Back_populates= "children") class Parent (Base): _ _ tablename__ = 'left' id = Column (Integer, primary_key=True) children = relationship ("Association", back_populates= "parent") class Child (Base): _ _ tablename__ =' right' id = Column (Integer, primary_key=True) parents = relationship ("Association", back_populates= "child") # create parent Append a child via associationp = Parent () a = Association (extra_data= "some data") a.child = Child () p.children.append (a) # iterate through child objects via association, including association# attributesfor assoc in p.children: print (assoc.extra_data) print (assoc.child)
# be patient and attentive on the way to find the answer!
It should be noted that the back_populates parameter assignment parameter must not be the field of the first parameter of relationship, which is equivalent to a duplicate field in the corresponding relation table.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.