from sqlalchemy import Column, ForeignKey, MetaData, String, Table from sqlalchemy.orm import backref, registry, relationship from backend.model.assessment import Assessment from backend.model.student import Student from backend.model.tribe import Tribe metadata = MetaData() mapper_registry = registry() tribes_table = Table( "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")), ) 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)