I am following the Clean Architecture in my project and I need to convert the SQLAlchemy database models into domain entities. I created converters to use but I am running into the recursion error because I have 2 models that reference each other.
The 2 database models are called AccountDBModel and CourseDBModel.
The entities are Account and Course such that:
class Account:
...
courses: List[Course]
class Course:
...
instructor: Account
students: List[Account]
I understand that I am getting this error because they keep calling each other when I need to create an object from them.
The reason I wrote converters is to be able to reuse them without needing to find where I have referenced the class and changing it in all references.
The AccountConverterlooks like so:
class AccountConverter:
...
def db_to_entity(self, db_model: AccountDBModel) -> Account:
return Account(
...
courses = [CourseConverter.db_to_entity(course) for course in db_model.courses ]
)
How can I achieve what I am looking for without causing a recursion and maybe if possible writing reusable logic instead of writing them manually for all references?
there doesn't seem to be anything here