-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmany2many.py
More file actions
35 lines (30 loc) · 1.03 KB
/
many2many.py
File metadata and controls
35 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from sqlalchemy import Table, Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
association_table = Table('association', Base.metadata,
Column('men_id', Integer, ForeignKey('men.id')),
Column('women_id', Integer, ForeignKey('women.id'))
)
class Boys(Base):
__tablename__ = 'men'
id = Column(Integer, primary_key=True)
name = Column(String(25))
girls = relationship(
"Girls",
secondary=association_table,
back_populates="boys")
class Girls(Base):
__tablename__ = 'women'
id = Column(Integer, primary_key=True)
name = Column(String(25))
boys = relationship(
"Boys",
secondary=association_table,
back_populates="girls")
engine = create_engine('mysql+pymysql://root@localhost:3306/testMtoM')
session = sessionmaker()
session.configure(bind=engine)
Base.metadata.create_all(engine)