<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 ORM - Basic Relationship Patterns
Following are the relationship patters found in the real world.
One to One
Step1:  One To Many
A one to many relationship places a foreign key on the Many table referencing the One. relationship() is then specified on the One, as referencing a collection of items represented by the Many
Step2:  To establish a bidirectional relationship in one-to-many, where the “reverse” side is a many to one, specify an additional relationship() and connect the two using the relationship.back_populates parameter
Step3:  TIP
Step4:  Child will get a parent attribute with many-to-one semantics.
Alternatively, the backref option may be used on a single relationship() instead of using back_populates
Step5:  Many To One
Tips
Step6:  Alternatively, the backref parameter may be applied to a single relationship(), such as Parent.child
Step7:  One To One
One To One is a bidirectional relationship with a scalar attribute on both sides. The uselist flag indicates the placement of a scalar attribute of the relationship. 
TIP
Step8:  Or for many-to-one
Step9:  Many To Many
Many to Many adds an association table between two classes. The association table is indicated by the secondary argument to relationship(). Usually, the Table uses the MetaData object associated with the declarative base class, so that the ForeignKey directives can locate the remote tables with which to link
Step10:  For a bidirectional relationship, both sides of the relationship contain a collection. Specify using relationship.back_populates, and for each relationship() specify the common association table
Step11:  The secondary argument of relationship() also accepts a callable that returns the ultimate argument, which is evaluated only when mappers are first used. Using this, we can define the association_table at a later point, as long as it’s available to the callable after all module initialization is complete
Step12:  With the declarative extension in use, the traditional “string name of the table” is accepted as well, matching the name of the table as stored in Base.metadata.tables
Step13:  Deleting Rows from the Many to Many Table
A behavior which is unique to the secondary argument to relationship() is that the Table which is specified here is automatically subject to INSERT and DELETE statements, as objects are added or removed from the collection. There is no need to delete from this table manually. The act of removing a record from the collection will have the effect of the row being deleted on flush
Step14:  A question which often arises is how the row in the “secondary” table can be deleted when the child object is handed directly to Session.delete()
<ASSISTANT_TASK:>
Python Code:
# SQLAlchemy
from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Date, Integer, String
Base = declarative_base()
Explanation: ORM - Basic Relationship Patterns
Following are the relationship patters found in the real world.
One to One: Example - School/Head Master 
One to Many: Example - Teacher/students, office/employees, office/departments, school/Students
Many to One: students/school, employees/office, departments/office, cities/state  
Many to Many: Friends 
Lets see how they are impleted in SQLAlchemy and SQLObject
End of explanation
class One(Base):
    __tablename__ = 'one'
    id = Column(Integer, primary_key=True)
    many = relationship("Many")
class Many(Base):
    __tablename__ = 'many'
    id = Column(Integer, primary_key=True)
    one_id = Column(Integer, ForeignKey('one.id'))
Explanation: One To Many
A one to many relationship places a foreign key on the Many table referencing the One. relationship() is then specified on the One, as referencing a collection of items represented by the Many:
End of explanation
class person(Base):
    __tablename__ = 'pserson'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    address = relationship("address")
class address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('pserson.id'))
    street_name = Column(String)
    
p = person()
p.name = "Mayank"
p.address.street_name = "200 Timbaktu"
Explanation: To establish a bidirectional relationship in one-to-many, where the “reverse” side is a many to one, specify an additional relationship() and connect the two using the relationship.back_populates parameter:
End of explanation
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")
Explanation: TIP: Touch/update the individual resource and not the shared resource, address is shared resource and person is individual resource.
End of explanation
class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child", backref="parent")
Explanation: Child will get a parent attribute with many-to-one semantics.
Alternatively, the backref option may be used on a single relationship() instead of using back_populates:
End of explanation
class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'))
    child = relationship("Child")
class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
# Bidirectional behavior 
class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'))
    child = relationship("Child", back_populates="parents")
class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parents = relationship("Parent", back_populates="child")
Explanation: Many To One
Tips: 
- Plase a foreign key in the parent table referencing the one. 
- relationship is declared on the many, where a new scalar-holding attribute will be created
- Bidirectional behavior can be achieved by adding  relationship() in one and applying the relationship.back_populates parameter in both directions
In the below examples parents are many and a single child (Check many2one folder for examples)
End of explanation
class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'))
    child = relationship("Child", backref="parents")
Explanation: Alternatively, the backref parameter may be applied to a single relationship(), such as Parent.child:
End of explanation
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'))
    parent = relationship("Parent", back_populates="child")
Explanation: One To One
One To One is a bidirectional relationship with a scalar attribute on both sides. The uselist flag indicates the placement of a scalar attribute of the relationship. 
TIP: Create a one/Many relationship and add uselist flag in relation ship
End of explanation
from sqlalchemy.orm import backref
class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child_id = Column(Integer, ForeignKey('child.id'))
    child = relationship("Child", backref=backref("parent", uselist=False))
Explanation: Or for many-to-one:
As always, the relationship.backref and backref() functions may be used in lieu of the relationship.back_populates approach; to specify uselist on a backref, use the backref() function:
End of explanation
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)
class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)
Explanation: Many To Many
Many to Many adds an association table between two classes. The association table is indicated by the secondary argument to relationship(). Usually, the Table uses the MetaData object associated with the declarative base class, so that the ForeignKey directives can locate the remote tables with which to link:
End of explanation
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")
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,
                    backref="parents")
class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)
Explanation: For a bidirectional relationship, both sides of the relationship contain a collection. Specify using relationship.back_populates, and for each relationship() specify the common association table:
End of explanation
class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship("Child",
                    secondary=lambda: association_table,
                    backref="parents")
Explanation: The secondary argument of relationship() also accepts a callable that returns the ultimate argument, which is evaluated only when mappers are first used. Using this, we can define the association_table at a later point, as long as it’s available to the callable after all module initialization is complete:
End of explanation
class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship("Child",
                    secondary="association",
                    backref="parents")
Explanation: With the declarative extension in use, the traditional “string name of the table” is accepted as well, matching the name of the table as stored in Base.metadata.tables:
End of explanation
# row will be deleted from the "secondary" table
# automatically
myparent.children.remove(somechild)
Explanation: Deleting Rows from the Many to Many Table
A behavior which is unique to the secondary argument to relationship() is that the Table which is specified here is automatically subject to INSERT and DELETE statements, as objects are added or removed from the collection. There is no need to delete from this table manually. The act of removing a record from the collection will have the effect of the row being deleted on flush:
End of explanation
session.delete(somechild)
Explanation: A question which often arises is how the row in the “secondary” table can be deleted when the child object is handed directly to Session.delete():
End of explanation
<END_TASK>