Contents

Engineering Craft › Design Patterns · also in Relational Databases & SQL

Active Record

Objects that know how to save themselves to the database.

Also known as: Active Record pattern, ActiveRecord

Active Record is a pattern where an object represents a row in a database table and also knows how to load, save and delete itself. The class holds the data and the persistence code together, so user.save() writes the user’s row.

class User:
    def __init__(self, id=None, name=None):
        self.id = id
        self.name = name

    def save(self, db):
        if self.id is None:
            self.id = db.insert("users", {"name": self.name})
        else:
            db.update("users", self.id, {"name": self.name})

Frameworks such as Ruby on Rails popularized the approach, and the result is a short, readable path from code to database.

The trade-off is coupling. The domain object now depends on the database layout, so its tests need persistence, and the business rules are mixed with SQL-shaped concerns. A change to a column can ripple through the object’s logic. For simple records with little behaviour, that coupling costs little. For a rich domain, it becomes a burden.

The classic mistake is using Active Record for objects with complex rules and many relationships, then finding the business logic tangled with saving and loading. The alternative is a data mapper or repository, where the object knows nothing about the database and a separate class moves it in and out. Choose by how much behaviour the object carries. Keep the schema and its constraints in the database either way.