ORM SQLAlchemy table-to-table relationships

Source: Internet
Author: User

1 table and table three relationships

1.1 One-to-one relations

Example: A husband who corresponds to a wife, a wife to a husband

1.2 One-to-many relationships

Example: A person can have more than one car, asking for a search for all the vehicles owned by someone

Analysis: This can actually be a table, but because a person can have more than one car, if the use of a table, will result in redundant information too much. A good design is that people and vehicles are separately built, so how do you associate two tables? There is a clever way to add a foreign key field (person's number) to the vehicle's table.

1.3 Many-to-many relationships

For example, a student chooses a course and a student can take multiple courses, each of which can be chosen by multiple students.
Analysis: This approach can be modeled as a one-to-many approach, but there are too many redundant information, and the good way is to separate the entity and the relationship and create the table separately, the entity table is the student table and the curriculum, the relational table is the elective table, where the relational table is based on the joint primary key and the curriculum primary key.

2 SQLAlchemy Table Relationship Usage

2.1 Pair of Multi

Like a father can have more than one child,

ClassParent(Base):__tablename__=' Parent 'Id=Column(Integer,Primary_key=True)Children= Relationship (  "child"  )  class child  (base): __tablename__ =  ' child ' id = column ( integerprimary_key=True)  parent_id = column  (integerforeignkey (  ' parent.id '            

One -to-many to establish a two-way relationship
ClassParent(Base):__tablename__=' Parent 'Id=Column(Integer,Primary_key=True)Children= Relationship("Child", back_populates="parent")ClassChild(Base):__tablename__=  ' child ' id = column  (integerprimary_key= true) parent_id = column  (integerforeignkey (  ' parent.id ' ))   parent  = relationship< Span class= "P" > ( "Parent" back_populates= " children " ) 

2.2-to-one

Like a father can have more than one child,

< Span class= "p" > < Span class= "p" > < span class= "n" >  

/span>

ORM SQLAlchemy table-to-table relationships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.