49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker, relationship
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
teachers_lessons = Table(
|
|
"teachers_lessons",
|
|
Base.metadata,
|
|
Column("fk_teacher", Integer, ForeignKey("teachers.id")),
|
|
Column("fk_lesson", Integer, ForeignKey("lessons.id")),
|
|
)
|
|
|
|
|
|
class Teacher(Base):
|
|
__tablename__ = "teachers"
|
|
|
|
id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True)
|
|
name = Column("name", String(50), nullable=False)
|
|
|
|
lessons = relationship(
|
|
"Lesson",
|
|
backref="teachers",
|
|
secondary=teachers_lessons
|
|
)
|
|
|
|
|
|
class Lesson(Base):
|
|
__tablename__ = "lessons"
|
|
|
|
id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True)
|
|
name = Column("name", String(50), nullable=False)
|
|
|
|
|
|
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True)
|
|
Base.metadata.create_all(engine)
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
t2 = s.query(Teacher).filter_by(name='NEWNEW').first()
|
|
t2.lessons.clear()
|
|
s.commit()
|
|
|
|
|