cosmicpython / code

Example application code for the python architecture book
Other
2.07k stars 922 forks source link

Is there a reason why we don't use sql alchemy orm to create database models? #67

Closed uguryilmazunified closed 1 year ago

uguryilmazunified commented 1 year ago

hello, amazing book so far. I really enjoy what i am reading. Just one thing i am trying to wrap my head around

why do we have a domain level separately and we use mappers to declaratively map database models to those domain models. I usually use SqlAlchemy models and domain kind of connected together by inheriting a Model class. something like this -for us we would have the same classes for product or order line

# class StudentClasses(Base):
#     __tablename__ = "student_classes"

#     id = Column(Integer, primary_key=True)
#     student_id = Column(Integer, ForeignKey('student.id'))
#     class_id = Column(Integer, ForeignKey('classes.id'))

i understand that having a domain level almost acts as a documentation for new developers who join to projects. It easily tells what's going on at our core domain logic but it just feels like we are also creating more complexity. I want to understand if there is more advantage to using mapper and table from sql alchemy instead of ORM funcitonalities. Thanks!

jalvespinto commented 1 year ago

Decoupling I believe. I really don't have much programming experience, but if you google it you will find an infinity of reasons why not. I ended up using ORM, but than having a mapping from/to domain/orm instances. Here a two places where you can see what I mean: