Fully functional implementation of the foreignKey
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
from uuid import UUID, uuid4
|
||||
from enum import Enum
|
||||
|
||||
from sqlmodel import Field, SQLModel, select
|
||||
from sqlmodel import Field, SQLModel, select, Relationship
|
||||
from pydantic import Field as PydField
|
||||
|
||||
from category.models import CategoryRead
|
||||
from category.models import CategoryRead, Category
|
||||
|
||||
|
||||
class AccountType(Enum):
|
||||
@@ -38,6 +38,7 @@ class AccountBaseId(AccountBase):
|
||||
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
||||
|
||||
class Account(AccountBaseId, table=True):
|
||||
default_category: Category | None = Relationship()
|
||||
|
||||
@classmethod
|
||||
def create(cls, account, session):
|
||||
@@ -50,7 +51,7 @@ class Account(AccountBaseId, table=True):
|
||||
|
||||
@classmethod
|
||||
def list(cls):
|
||||
return select(Account)
|
||||
return select(Account).join(Category)
|
||||
|
||||
@classmethod
|
||||
def get(cls, session, account_id):
|
||||
@@ -70,8 +71,7 @@ class Account(AccountBaseId, table=True):
|
||||
session.commit()
|
||||
|
||||
class AccountRead(AccountBaseId):
|
||||
# default_category: CategoryRead
|
||||
pass
|
||||
default_category: CategoryRead
|
||||
|
||||
class AccountWrite(AccountBase):
|
||||
default_category_id: UUID = PydField(default=None, json_schema_extra={
|
||||
|
||||
Reference in New Issue
Block a user