|
| 1 | +from fastapi import FastAPI |
| 2 | +from fastapi_quickcrud import crud_router_builder |
| 3 | +from sqlalchemy import * |
| 4 | +from sqlalchemy.orm import * |
| 5 | + |
| 6 | +Base = declarative_base() |
| 7 | + |
| 8 | +association_table = Table('association', Base.metadata, |
| 9 | + Column('left_id', ForeignKey('left.id'), primary_key=True), |
| 10 | + Column('right_id', ForeignKey('right.id'), primary_key=True) |
| 11 | +) |
| 12 | + |
| 13 | +class Parent(Base): |
| 14 | + __tablename__ = 'left' |
| 15 | + id = Column(Integer, primary_key=True) |
| 16 | + children = relationship( |
| 17 | + "Child", |
| 18 | + secondary=association_table, |
| 19 | + back_populates="parents") |
| 20 | + |
| 21 | +class Child(Base): |
| 22 | + __tablename__ = 'right' |
| 23 | + id = Column(Integer, primary_key=True) |
| 24 | + parents = relationship( |
| 25 | + "Parent", |
| 26 | + secondary=association_table, |
| 27 | + back_populates="children") |
| 28 | + |
| 29 | +crud_route_1 = crud_router_builder(db_model=Parent, |
| 30 | + prefix="/parent", |
| 31 | + tags=["Parent"], |
| 32 | + foreign_include=[Child] |
| 33 | + ) |
| 34 | +crud_route_2 = crud_router_builder(db_model=Child, |
| 35 | + prefix="/child", |
| 36 | + tags=["Child"], |
| 37 | + foreign_include=[Parent] |
| 38 | + ) |
| 39 | +app = FastAPI() |
| 40 | +[app.include_router(i) for i in [crud_route_1, crud_route_2]] |
| 41 | + |
| 42 | + |
| 43 | +import uvicorn |
| 44 | +uvicorn.run(app, host="0.0.0.0", port=8002, debug=False) |
0 commit comments