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