ActiveRecord vs DataMapper in Python

Yasser Sinjab
1 min readOct 19, 2018

Many programmers use ORM for data persistence, but also many don’t know the patterns these ORMs internally use. In this story I will show the difference between two famous data persistence patterns:

  • Active Record: The object in this pattern encapsulates two main things: data and behavior. Since all business logic in the domain object it is clear and obvious where to access the database. Django ORM is one the famous libraries that follow this approach
  • Data mapper: a data mapper is layer that is responsible for transferring the data between a data store and in-memory objects. SQLAlchemy follows this patterns.

I will post two gists that includes clear examples of how the libraries mentioned above apply these patterns.

  • SQLAlchemy
  • Django ORM:
    I created a django project with an application called users. After I ran makemigrations and migrate for creating the tables, I applied the code in the following gist.

--

--

Yasser Sinjab

Software Engineer. Data nerd. Machine learning enthusiast.