[WIP] All get, post, update, delete operations with relations working

However, all models are contained in one file for easier debugging. Trying to refactor this back to multiple files
This commit is contained in:
linuskmr 2021-12-25 19:59:51 +01:00
parent 2255f9edcd
commit 426d34a2d3
8 changed files with 92 additions and 108 deletions

View file

@ -1,8 +0,0 @@
from typing import Optional
from sqlmodel import SQLModel, Field
class RecordTagLink(SQLModel, table=True):
record_id: Optional[int] = Field(default=None, foreign_key="record.id", primary_key=True)
tag_id: Optional[int] = Field(default=None, foreign_key="tag.id", primary_key=True)

View file

@ -0,0 +1,89 @@
from datetime import datetime, timedelta
from typing import Optional, List
from sqlmodel import SQLModel, Field, Relationship
class RecordTagLink(SQLModel, table=True):
record_id: Optional[int] = Field(default=None, foreign_key="record.id", primary_key=True)
tag_id: Optional[int] = Field(default=None, foreign_key="tag.id", primary_key=True)
class ProjectBase(SQLModel):
"""Superclass model which all project classes have in common."""
name: str
creation_date: datetime = Field(default=datetime.now())
class ProjectCreate(ProjectBase):
"""Model used when a user creates a new project."""
pass
class Project(ProjectBase, table=True):
"""Model used inside the database."""
id: Optional[int] = Field(default=None, primary_key=True)
records: List["Record"] = Relationship(back_populates="project")
class ProjectRead(ProjectBase):
"""Model used when querying information about a module."""
id: int
creation_date: datetime
class RecordBase(SQLModel):
"""Superclass model that all record classes have in common."""
start: datetime = Field(default=datetime.now())
end: Optional[datetime] = Field(default=None)
class RecordCreate(RecordBase):
"""Model used when a user creates a record."""
project_id: int
tags: List[int] = Field(default=list())
class Record(RecordBase, table=True):
"""Model used inside the database."""
id: Optional[int] = Field(default=None, primary_key=True)
project: Optional[Project] = Relationship(back_populates="records")
project_id: Optional[int] = Field(default=None, foreign_key="project.id")
tags: List["Tag"] = Relationship(back_populates="records", link_model=RecordTagLink)
class RecordRead(RecordBase):
"""Model used when a user queries a record."""
id: int
project_id: int
class TagBase(SQLModel):
"""Superclass model which all tag classes have in common."""
name: str
class TagCreate(TagBase):
"""Model used when creating a new model."""
pass
class Tag(TagBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
records: List[Record] = Relationship(back_populates="tags", link_model=RecordTagLink)
class TagRead(TagBase):
"""Model used when querying information about a tag."""
id: int
name: str

View file

@ -1,31 +0,0 @@
from datetime import datetime, timedelta
from typing import Optional
from sqlmodel import SQLModel, Field, Relationship
class ProjectBase(SQLModel):
"""Superclass model which all project classes have in common."""
name: str
creation_date: datetime = Field(default=datetime.now())
class ProjectCreate(ProjectBase):
"""Model used when a user creates a new project."""
pass
class Project(ProjectBase, table=True):
"""Model used inside the database."""
id: Optional[int] = Field(default=None, primary_key=True)
records: list["Record"] = Relationship(back_populates="project")
class ProjectRead(ProjectBase):
"""Model used when querying information about a module."""
id: int
creation_date: datetime
records: list[int]

View file

@ -1,36 +0,0 @@
from datetime import datetime
from typing import Optional
from sqlmodel import SQLModel, Field, Relationship
from src.models.links import RecordTagLink
class RecordBase(SQLModel):
"""Superclass model that all record classes have in common."""
start: datetime = Field(default=datetime.now())
end: Optional[datetime] = Field(default=None)
class RecordCreate(RecordBase):
"""Model used when a user creates a record."""
project: int
tags: list[int] = Field(default=list())
class Record(RecordBase, table=True):
"""Model used inside the database."""
id: Optional[int] = Field(default=None, primary_key=True)
project: Optional["Project"] = Relationship(back_populates="records")
tags: list["Tag"] = Relationship(back_populates="records", link_model=RecordTagLink)
class RecordRead(RecordBase):
"""Model used when a user queries a record."""
id: int
project: int
tags: list[int]

View file

@ -1,30 +0,0 @@
from datetime import datetime, timedelta
from typing import Optional
from sqlmodel import SQLModel, Field, Relationship
from src.models.links import RecordTagLink
class TagBase(SQLModel):
"""Superclass model which all tag classes have in common."""
name: str
records: list["Record"] = Relationship(back_populates="tags", link_model=RecordTagLink)
class TagCreate(TagBase):
"""Model used when creating a new model."""
pass
class Tag(TagBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class TagRead(TagBase):
"""Model used when querying information about a tag."""
id: int
name: str

View file

@ -6,7 +6,7 @@ from fastapi import APIRouter, status, Path, Depends, HTTPException
from sqlmodel import Session, select
from src import database
from src.models.project import ProjectCreate, ProjectRead, Project
from src.models.models import ProjectCreate, ProjectRead, Project
router = APIRouter(prefix='/projects', tags=["Projects"])

View file

@ -2,7 +2,7 @@ from fastapi import APIRouter, status, Path, Depends, HTTPException
from sqlmodel import select, Session
from src import database
from src.models.record import Record, RecordRead, RecordCreate
from src.models.models import Record, RecordRead, RecordCreate
router = APIRouter(prefix="/records", tags=["Records"])

View file

@ -6,7 +6,7 @@ from fastapi import APIRouter, status, Path, Depends, HTTPException
from sqlmodel import Session, select
from src import database
from src.models.tag import TagRead, TagCreate, Tag
from src.models.models import TagRead, TagCreate, Tag
router = APIRouter(prefix="/tags", tags=["Tags"])