recopytex/backend/adapters/orm.py

50 lines
1.4 KiB
Python
Raw Permalink Normal View History

from sqlalchemy import Column, ForeignKey, MetaData, String, Table
from sqlalchemy.orm import backref, registry, relationship
2022-12-20 09:25:12 +00:00
from backend.model.assessment import Assessment
from backend.model.student import Student
2022-12-21 05:53:27 +00:00
from backend.model.tribe import Tribe
2022-12-20 09:25:12 +00:00
metadata = MetaData()
mapper_registry = registry()
2022-12-20 09:25:12 +00:00
2022-12-21 05:53:27 +00:00
tribes_table = Table(
2022-12-20 09:25:12 +00:00
"tribes",
metadata,
Column("name", String(255), primary_key=True),
Column("level", String(255)),
)
assessments_table = Table(
"assessments",
metadata,
Column("id", String(255), primary_key=True),
Column("name", String(255)),
Column("tribe_name", String(255), ForeignKey("tribes.name")),
)
students_table = Table(
"students",
metadata,
Column("id", String(255), primary_key=True),
Column("name", String(255)),
Column("tribe_name", String(255), ForeignKey("tribes.name")),
)
2022-12-20 09:25:12 +00:00
def start_mappers():
tribes_mapper = mapper_registry.map_imperatively(
Tribe,
tribes_table,
properties={
"students": relationship(
Student, backref="tribes", order_by=students_table.c.id
),
"assessments": relationship(
Assessment, backref="tribes", order_by=assessments_table.c.id
),
},
)
students_mapper = mapper_registry.map_imperatively(Student, students_table)
assessments_mapper = mapper_registry.map_imperatively(Assessment, assessments_table)