CINXE.COM
Active Record
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta content = 'uft-8' name = 'charset'></meta> <title>Active Record</title> <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> <meta content = 'summary' name = 'twitter:card'></meta> <meta content = '16665197' name = 'twitter:site:id'></meta> <meta content = '@martinfowler' name = 'twitter:site'></meta> <meta content = 'Active Record' property = 'og:title'></meta> <meta content = 'https://martinfowler.com/eaaCatalog/activeRecord.html' property = 'og:url'></meta> <meta content = 'An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.' property = 'og:description'></meta> <meta content = 'https://martinfowler.com/logo-sq.png' property = 'og:image'></meta> <meta content = 'martinfowler.com' property = 'og:site_name'></meta> <meta content = 'article' property = 'og:type'></meta> <meta content = '2003-03-05' property = 'og:article:modified_time'></meta> <meta content = 'width=device-width, initial-scale=1' name = 'viewport'></meta> <link href = 'index.css' rel = 'stylesheet' type = 'text/css'></link> </head> <body><header id = 'banner' style = 'background-image: url("/banner.png"); background-repeat: no-repeat'> <div class = 'name-logo'><a href = 'https://martinfowler.com'><img src = '/mf-name-white.png'></img></a></div> <div class = 'search'> <!-- SiteSearch Google --> <form method='GET' action="https://www.google.com/search"> <input type='hidden' name='ie' value='UTF-8'/> <input type='hidden' name='oe' value='UTF-8'/> <input class = 'field' type='text' name='q' size='15' maxlength='255' value=""/> <button class = 'button' type='submit' name='btnG' value=" " title = "Search"/> <input type='hidden' name='domains' value="martinfowler.com"/> <input type='hidden' name='sitesearch' value=""/> <input type='hidden' name='sitesearch' value="martinfowler.com"/> </form> </div> <div class = 'menu-button navmenu-button'><a class = 'icon icon-bars' href = '#navmenu-bottom'></a></div> <nav class = 'top-menu'> <ul> <li><a class = '' href = 'https://refactoring.com'>Refactoring</a></li> <li><a class = '' href = '/agile.html'>Agile</a></li> <li><a class = '' href = '/architecture'>Architecture</a></li> <li><a class = '' href = '/aboutMe.html'>About</a></li> <li><a class = 'tw' href = 'https://www.thoughtworks.com'>Thoughtworks</a></li> <li><a class = 'icon icon-rss' href = '/feed.atom' title = 'feed'></a></li> <li><a class = 'icon icon-twitter' href = 'https://www.twitter.com/martinfowler' title = 'Twitter stream'></a></li> <li class = 'icon'><a href = 'https://toot.thoughtworks.com/@mfowler' title = 'Mastodon stream'><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M21.2595 13.9898C20.9852 15.4006 18.8033 16.9446 16.2974 17.2439C14.9907 17.3998 13.7041 17.5431 12.3321 17.4802C10.0885 17.3774 8.31809 16.9446 8.31809 16.9446C8.31809 17.163 8.33156 17.371 8.3585 17.5655C8.65019 19.7797 10.5541 19.9124 12.3576 19.9742C14.1779 20.0365 15.7987 19.5254 15.7987 19.5254L15.8735 21.1711C15.8735 21.1711 14.6003 21.8548 12.3321 21.9805C11.0814 22.0493 9.52849 21.9491 7.71973 21.4703C3.79684 20.432 3.12219 16.2504 3.01896 12.0074C2.98749 10.7477 3.00689 9.55981 3.00689 8.56632C3.00689 4.22771 5.84955 2.95599 5.84955 2.95599C7.2829 2.29772 9.74238 2.0209 12.2993 2H12.3621C14.919 2.0209 17.3801 2.29772 18.8133 2.95599C18.8133 2.95599 21.6559 4.22771 21.6559 8.56632C21.6559 8.56632 21.6916 11.7674 21.2595 13.9898ZM18.3029 8.9029C18.3029 7.82924 18.0295 6.97604 17.4805 6.34482C16.9142 5.71359 16.1726 5.39001 15.2522 5.39001C14.187 5.39001 13.3805 5.79937 12.8473 6.61819L12.3288 7.48723L11.8104 6.61819C11.2771 5.79937 10.4706 5.39001 9.40554 5.39001C8.485 5.39001 7.74344 5.71359 7.17719 6.34482C6.62807 6.97604 6.3547 7.82924 6.3547 8.9029V14.1562H8.43597V9.05731C8.43597 7.98246 8.88822 7.4369 9.79281 7.4369C10.793 7.4369 11.2944 8.08408 11.2944 9.36376V12.1547H13.3634V9.36376C13.3634 8.08408 13.8646 7.4369 14.8648 7.4369C15.7694 7.4369 16.2216 7.98246 16.2216 9.05731V14.1562H18.3029V8.9029Z"></path></svg> </a></li> <li class = 'icon'><a href = 'https://www.linkedin.com/in/martin-fowler-com/' title = 'LinkedIn'><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M4.00098 3H20.001C20.5533 3 21.001 3.44772 21.001 4V20C21.001 20.5523 20.5533 21 20.001 21H4.00098C3.44869 21 3.00098 20.5523 3.00098 20V4C3.00098 3.44772 3.44869 3 4.00098 3ZM5.00098 5V19H19.001V5H5.00098ZM7.50098 9C6.67255 9 6.00098 8.32843 6.00098 7.5C6.00098 6.67157 6.67255 6 7.50098 6C8.3294 6 9.00098 6.67157 9.00098 7.5C9.00098 8.32843 8.3294 9 7.50098 9ZM6.50098 10H8.50098V17.5H6.50098V10ZM12.001 10.4295C12.5854 9.86534 13.2665 9.5 14.001 9.5C16.072 9.5 17.501 11.1789 17.501 13.25V17.5H15.501V13.25C15.501 12.2835 14.7175 11.5 13.751 11.5C12.7845 11.5 12.001 12.2835 12.001 13.25V17.5H10.001V10H12.001V10.4295Z"></path></svg> </a></li> <li class = 'icon'><a href = 'https://bsky.app/profile/martinfowler.com' title = 'BlueSky'><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M12 11.3884C11.0942 9.62673 8.62833 6.34423 6.335 4.7259C4.13833 3.17506 3.30083 3.4434 2.75167 3.69256C2.11583 3.9784 2 4.95506 2 5.52839C2 6.10339 2.315 10.2367 2.52 10.9276C3.19917 13.2076 5.61417 13.9776 7.83917 13.7309C4.57917 14.2142 1.68333 15.4017 5.48083 19.6292C9.65833 23.9542 11.2058 18.7017 12 16.0392C12.7942 18.7017 13.7083 23.7651 18.4442 19.6292C22 16.0392 19.4208 14.2142 16.1608 13.7309C18.3858 13.9784 20.8008 13.2076 21.48 10.9276C21.685 10.2376 22 6.10256 22 5.52923C22 4.95423 21.8842 3.97839 21.2483 3.6909C20.6992 3.44256 19.8617 3.17423 17.665 4.72423C15.3717 6.34506 12.9058 9.62756 12 11.3884Z"></path></svg></a></li> </ul> </nav> </header> <nav id = 'top-navmenu'> <nav class = 'navmenu'> <div class = 'nav-head'> <div class = 'search'> <!-- SiteSearch Google --> <form method='GET' action="https://www.google.com/search"> <input type='hidden' name='ie' value='UTF-8'/> <input type='hidden' name='oe' value='UTF-8'/> <input class = 'field' type='text' name='q' size='15' maxlength='255' value=""/> <button class = 'button' type='submit' name='btnG' value=" " title = "Search"/> <input type='hidden' name='domains' value="martinfowler.com"/> <input type='hidden' name='sitesearch' value=""/> <input type='hidden' name='sitesearch' value="martinfowler.com"/> </form> </div> <div class = 'closediv'> <span class = 'close' title = 'close'></span> </div> </div> <div class = 'nav-body'> <div class = 'topics'> <h2>Topics</h2> <p><a href = '/architecture'>Architecture</a></p> <p><a href = 'https://refactoring.com'>Refactoring</a></p> <p><a href = '/agile.html'>Agile</a></p> <p><a href = '/delivery.html'>Delivery</a></p> <p><a href = '/microservices'>Microservices</a></p> <p><a href = '/data'>Data</a></p> <p><a href = '/testing'>Testing</a></p> <p><a href = '/dsl.html'>DSL</a></p> </div> <div class = 'about'> <h2>about me</h2> <p><a href = '/aboutMe.html'>About</a></p> <p><a href = '/books'>Books</a></p> <p><a href = '/faq.html'>FAQ</a></p> </div> <div class = 'content'> <h2>content</h2> <p><a href = '/videos.html'>Videos</a></p> <p><a href = '/tags'>Content Index</a></p> <p><a href = '/articles/eurogames'>Board Games</a></p> <p><a href = '/photos'>Photography</a></p> </div> <div class = 'tw'> <h2>Thoughtworks</h2> <p><a href = 'https://thoughtworks.com/insights'>Insights</a></p> <p><a href = 'https://thoughtworks.com/careers'>Careers</a></p> <p><a href = 'https://thoughtworks.com/radar'>Radar</a></p> </div> <div class = 'feeds'> <h2>follow</h2> <p><a href = '/feed.atom'>RSS</a></p> <p><a href = 'https://toot.thoughtworks.com/@mfowler'>Mastodon</a></p> <p><a href = 'https://www.linkedin.com/in/martin-fowler-com/'>LinkedIn</a></p> <p><a href = 'https://www.twitter.com/martinfowler'>X (Twitter)</a></p> <p><a href = 'https://boardgamegeek.com/blog/13064/martins-7th-decade'>BGG</a></p> </div> </div> </nav> </nav> <main class = 'page'> <div class = 'paperBody'> <h1 class = 'name'>Active Record</h1> <p class = 'intent'>An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.</p> <p><img src = 'index/activeRecordSketch.gif'></img></p> <p>An object carries both data and behavior. Much of this data is persistent and needs to be stored in a database. Active Record uses the most obvious approach, putting data access logic in the domain object. This way all people know how to read and write their data to and from the database.</p> <p class = 'oreilly'>for more details go to <a href = 'https://learning.oreilly.com/library/view/patterns-of-enterprise/0321127420/ch10.html#ch10lev1sec3'>Chapter 10</a> of the online ebook at oreilly.com</p> </div> <div class = 'side-panel'> <div class = 'page-placard'> <p><a href = '.'><img src = '/books/eaa.jpg'></img></a></p> <p>This pattern is part of <a href = '.'>Patterns of Enterprise Application Architecture</a></p> </div> <div class = 'author'> <address class = 'name'><a href = '/' rel = 'author'>Martin Fowler</a></address> </div> <p class = 'date'>05 March 2003</p> </div> </main> <nav id = 'bottom-navmenu'> <nav class = 'navmenu'> <div class = 'nav-head'> <div class = 'search'> <!-- SiteSearch Google --> <form method='GET' action="https://www.google.com/search"> <input type='hidden' name='ie' value='UTF-8'/> <input type='hidden' name='oe' value='UTF-8'/> <input class = 'field' type='text' name='q' size='15' maxlength='255' value=""/> <button class = 'button' type='submit' name='btnG' value=" " title = "Search"/> <input type='hidden' name='domains' value="martinfowler.com"/> <input type='hidden' name='sitesearch' value=""/> <input type='hidden' name='sitesearch' value="martinfowler.com"/> </form> </div> <div class = 'closediv'> <span class = 'close' title = 'close'></span> </div> </div> <div class = 'nav-body'> <div class = 'topics'> <h2>Topics</h2> <p><a href = '/architecture'>Architecture</a></p> <p><a href = 'https://refactoring.com'>Refactoring</a></p> <p><a href = '/agile.html'>Agile</a></p> <p><a href = '/delivery.html'>Delivery</a></p> <p><a href = '/microservices'>Microservices</a></p> <p><a href = '/data'>Data</a></p> <p><a href = '/testing'>Testing</a></p> <p><a href = '/dsl.html'>DSL</a></p> </div> <div class = 'about'> <h2>about me</h2> <p><a href = '/aboutMe.html'>About</a></p> <p><a href = '/books'>Books</a></p> <p><a href = '/faq.html'>FAQ</a></p> </div> <div class = 'content'> <h2>content</h2> <p><a href = '/videos.html'>Videos</a></p> <p><a href = '/tags'>Content Index</a></p> <p><a href = '/articles/eurogames'>Board Games</a></p> <p><a href = '/photos'>Photography</a></p> </div> <div class = 'tw'> <h2>Thoughtworks</h2> <p><a href = 'https://thoughtworks.com/insights'>Insights</a></p> <p><a href = 'https://thoughtworks.com/careers'>Careers</a></p> <p><a href = 'https://thoughtworks.com/radar'>Radar</a></p> </div> <div class = 'feeds'> <h2>follow</h2> <p><a href = '/feed.atom'>RSS</a></p> <p><a href = 'https://toot.thoughtworks.com/@mfowler'>Mastodon</a></p> <p><a href = 'https://www.linkedin.com/in/martin-fowler-com/'>LinkedIn</a></p> <p><a href = 'https://www.twitter.com/martinfowler'>X (Twitter)</a></p> <p><a href = 'https://boardgamegeek.com/blog/13064/martins-7th-decade'>BGG</a></p> </div> </div> </nav> </nav> <footer id='page-footer'> <div class='tw-logo'> <a href='http://www.thoughtworks.com'> <img src='/thoughtworks_white.png'> </a> </div> <div class='menu-button'> <div class='icon-bars navmenu-button'></div> </div> <div class='copyright'> <p>漏 Martin Fowler | <a href="http://www.thoughtworks.com/privacy-policy">Privacy Policy</a> | <a href="/aboutMe.html#disclosures">Disclosures</a></p> </div> </footer> <!-- Google Analytics --> <!-- old Google Universal --> <script> window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; ga('create', 'UA-17005812-1', 'auto'); ga('send', 'pageview'); </script> <script async src='https://www.google-analytics.com/analytics.js'></script> <!-- New Google GA4 --> <!-- global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-6D51F4BDVF"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-6D51F4BDVF'); </script> <!-- End Google Analytics --> <script src = '/jquery-1.11.3.min.js' type = 'text/javascript'></script> <script src = '/mfcom.js' type = 'text/javascript'></script> </body> </html>