-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone2one.py
More file actions
26 lines (21 loc) · 864 Bytes
/
one2one.py
File metadata and controls
26 lines (21 loc) · 864 Bytes
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
from sqlalchemy import Table, Column, Integer, String, 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()
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(25))
child = relationship("Child", back_populates="parent", uselist=False)
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(25))
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", back_populates="child")
engine = create_engine('mysql+pymysql://root@localhost:3306/test1to1')
session = sessionmaker()
session.configure(bind=engine)
Base.metadata.create_all(engine)