<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>a work on process &#187; orm</title>
	<atom:link href="http://jystewart.net/process/tag/orm/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Tue, 27 Jul 2010 09:53:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using the Django ORM as a standalone component</title>
		<link>http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/</link>
		<comments>http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 23:55:37 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[sqlobject]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/</guid>
		<description><![CDATA[I&#8217;ve found myself working in Python lately, both for a new project and while preparing a review of the Django book. Working in Ruby I&#8217;ve become used to relying on ActiveRecord whenever I need to talk to a database (whether inside Rails or not) and after a little time refamiliarising myself with MySQLdb I realised <a href="http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p><img src='http://jystewart.net/process/wp-content/uploads/2008/02/picture-2.png' alt='Django logo' />I&#8217;ve found myself working in Python lately, both for a new project and while preparing a review of <a href="http://thedjangobook.com">the Django book</a>. Working in Ruby I&#8217;ve become used to relying on ActiveRecord whenever I need to talk to a database (whether inside Rails or not) and after a little time refamiliarising myself with <a href="http://sourceforge.net/projects/mysql-python">MySQLdb</a> I realised I was going to need a decent ORM for this project. As well as being road-tested and well documented, I was also looking for something that could either generate its models on the fly, or had a tool to generate them from an existing schema.</p>
<p>I checked out <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> and <a href="http://www.sqlobject.org/">SQLObject</a>, both of which looked like worthy contendors, but couldn&#8217;t find the generator I was looking for (if you know of one, please do let me know in the comments!) so I switched over to <a href="http://www.djangoproject.com/">Django</a>. I couldn&#8217;t find much information on using their ORM standalone, so thought I should share what I discovered. Please bear in mind that my python is rather rusty&#8211;if there are better ways to do any of this I&#8217;d be very pleased to hear about it.</p>
<p>The tool to generate your model classes from an existing database is <em>django-admin.py</em> which should be in your path if you&#8217;ve got Django installed. To get that up and running you&#8217;re going to need to set a few options.</p>
<p>First up, you&#8217;ll need a <em>settings.py</em> file specifying your database details. Mine looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">DATABASE_ENGINE = <span style="color: #483d8b;">'mysql'</span>
DATABASE_NAME = <span style="color: #483d8b;">'mydatabase'</span>
DATABASE_USER = <span style="color: #483d8b;">'myusername'</span>
DATABASE_PASSWORD = <span style="color: #483d8b;">'mypassword'</span></pre></div></div>

<p>Once that is done and saved, then from the command line you should be able to call:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">django-admin.py inspectdb <span style="color: #660033;">--settings</span>=settings</pre></div></div>

<p>and the models will be echoed to stdout. To redirect that to a file called models.py you&#8217;ll need:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">django-admin.py inspectdb <span style="color: #660033;">--settings</span>=settings <span style="color: #000000; font-weight: bold;">&gt;</span> models.py</pre></div></div>

<p>I found a few areas where the generated objects threw errors. We have a column called &#8216;try&#8217; which is a python keyword, so that required a small change to the code. And a number of models have foreign key relationships with other models that are declared after them, so a little reorganising was called for. It&#8217;d be really nice to have the script handle that, but it wasn&#8217;t a big deal to make the changes by hand.</p>
<p>With that done, I tried a very simple new script:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> models <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
a = MyModel.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
<span style="color: #ff7700;font-weight:bold;">print</span> a</pre></div></div>

<p>But ran into a few issues:</p>
<p>The ORM is relying on the environment variable <strong>DJANGO_SETTINGS_MODULE</strong> to tell it where to find your database credentials. You&#8217;ll need to set that to point to the settings.py file you created earlier.</p>
<p>You can&#8217;t have your <em>models.py</em> file in the same folder as your test script. Doing so throws a &#8220;IndexError: list index out of range&#8221; error. Instead make a new folder called, say, orm and put <em>models.py</em> in it, along with an empty file called <em>__init__.py</em> which will tell python to treat the folder as a module. You can then update your test script to:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> orm.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
a = MyModel.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
<span style="color: #ff7700;font-weight:bold;">print</span> a</pre></div></div>

<p>With that done, you should be able to use the ORM just as you would in your Django view code. </p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>DataMapper &#8211; Competition for ActiveRecord?</title>
		<link>http://jystewart.net/process/2007/12/datamapper-competition-for-activerecord/</link>
		<comments>http://jystewart.net/process/2007/12/datamapper-competition-for-activerecord/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 01:30:53 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/2007/12/datamapper-competition-for-activerecord/</guid>
		<description><![CDATA[When Ruby on Rails first hit the scene, what attracted many of us to it was ActiveRecord. By providing a declarative syntax for describing relationships, validations, and callbacks it provided an elegance to model code that makes programming a lot more fun. Over the past couple of years ActiveRecord has received a lot of love, <a href="http://jystewart.net/process/2007/12/datamapper-competition-for-activerecord/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>When Ruby on Rails first hit the scene, what attracted many of us to it was <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">ActiveRecord</a>. By providing a declarative syntax for describing relationships, validations, and callbacks it provided an elegance to model code that makes programming a lot more fun. Over the past couple of years ActiveRecord has received a lot of love, with <em><a href="http://weblog.rubyonrails.com/2006/04/21/habtm-vs-has_many-through/" title="Riding Rails: habtm vs has_many :through">has_many :through</a></em> and <a href="http://ryandaigle.com/articles/2007/2/7/what-s-new-in-edge-rails-activerecord-explicit-caching" title="Ryan's Scraps: What's New in Edge Rails: ActiveRecord Explicit Caching">improved caching options</a> being the key additions I&#8217;ve enjoyed. But despite all that it&#8217;s given, ActiveRecord is clearly not the last word in Object Relational Mappers and it&#8217;s been good to see increased attention for some alternative Ruby <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" title="Object-relational mapping - Wikipedia, the free encyclopedia">ORM</a>s such as <a href="http://datamapper.org/" title="DataMapper">DataMapper</a>.</p>
<p>DataMapper shares a lot of syntax with ActiveRecord but promises a few new features, such as a more succinct find syntax, eg.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Find first entry in my_objects table</span>
  MyObject.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ActiveRecord</span>
  MyObject.<span style="color:#9900CC;">first</span>        <span style="color:#008000; font-style:italic;"># DataMapper</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Find all entries in my_objects table</span>
  MyObject.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#008000; font-style:italic;"># ActiveRecord</span>
  MyObject.<span style="color:#9900CC;">all</span>          <span style="color:#008000; font-style:italic;"># DataMapper</span></pre></div></div>

<p>and perhaps more usefully, improved finder options. Edited from the DataMapper site:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># 'gt' means greater-than. We also do 'lt'.</span>
Person.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:age</span>.<span style="color:#9900CC;">gt</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># 'gte' means greather-than-or-equal-to. We also do 'lte'.</span>
Person.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:age</span>.<span style="color:#9900CC;">gte</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span>
Person.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>.<span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bob'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># If the value of a pair is an Array, we do an IN-clause for you.</span>
Person.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>.<span style="color:#9900CC;">like</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'S%'</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Does a NOT IN () clause for you.</span>
Person.<span style="color:#9900CC;">all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>.<span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bob'</span>,<span style="color:#996600;">'rick'</span>,<span style="color:#996600;">'steve'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>You can approximate that functionality with <a href="http://rubyforge.org/projects/ez-where/" title="RubyForge: ez_where: Project Info">some Rails plugins</a>, and of course you can use the :conditions parameter to an ActiveRecord finder to do the same queries, but there&#8217;s something nice about being able to do it all in Ruby. (You can also fall back to ActiveRecord like syntax)</p>
<p>Most uses of DataMapper I&#8217;ve seen so far have been standalone, or in conjunction with merb, but I decided to give it a try by converting an existing rails app. Given how much similarity there is in syntax it seemed like it would be fairly easy to convert a few models and see how it compared. Unfortunately, it&#8217;s not that easy.</p>
<p>It&#8217;s pretty rare to build a rails app without plugins, and most plugins that affect models do so by extending ActiveRecord::Base. Since by using DataMapper you&#8217;re breaking away from ActiveRecord::Base, those plugins won&#8217;t work without invasive surgery. Similarly, DataMapper doesn&#8217;t support AR&#8217;s validation syntax and while it will soon have validations of its own, it looks unlikely that they&#8217;ll be compatible. It might well be possible to write a plugin that makes many other plugins work by examining ActiveRecord::Base.extended_by, and that could provide a translation layer to allow ActiveRecord validations to work with DataMapper, but that begins to be a lot of work for very little return.</p>
<p>Where DataMapper may really help Rails developers is in introducing more competition into the Ruby ORM world. By taking some radically different design decisions, particularly with regards to migrations and column specifications DataMapper allows us to see a different, but similarly elegant way of doing things, and hopefully ideas will percolate back into ActiveRecord just as AR has clearly inspired DataMapper. Personally I am looking forward to using DataMapper in my next non-rails ruby project, but found the exploration a useful reminder of how much of rails&#8217; success is in its coherency as a stack.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2007/12/datamapper-competition-for-activerecord/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
