GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.pythoncentral.io — Python's SQLAlchemy vs Other ORMs | Python Central Update: A review of PonyORM has been added.
Overview of Python ORMsAs a wonderful language, Python has lots of ORM libraries besides SQLAlchemy. In this article, we are going to take a look at several popular alternative ORM libraries to better understand the big picture of the Python ORM landscape. By writing a script that reads and writes to a simple database with two tables, SQLObjectSQLObject is a Python ORM that maps objects between a SQL database and Python. It is becoming more popular in the programming community due to its similarity to Ruby on Rails' ActiveRecord pattern. The first version of SQLObject was released in October 2002. It is licensed under LGPL. fake rolex daytona In SQLObject, database concepts are mapped into Python in a way that's very similar to SQLAlchemy, where tables are mapped as classes, rows as instances and columns as attributes. It also provides a Python-object-based query language that makes SQL more abstract, thus providing database agnosticity for applications. [shell] warning: no files found matching '*.html' warning: no files found matching '*.html' [python] The code above created two simple tables: [python] >>> a [/python]To get or retrieve the new records from the database, we use the magical [python] >>> list(persons) >>> list(addresses) >>> a1 = addresses[0] >>> a1 == a True [/python] StormStorm is a Python ORM that maps objects between one or more databases and Python. It allows developers to construct complex queries across multiple database tables to support dynamic storage and retrieval of object information. It was developed in Python at Canonical Ltd., the company behind Ubuntu, for use in the Launchpad and Landscape applications and subsequently released in 2007 as free software. The project is released under the LGPL license and contributors are required to assign copyrights to Canonical. Like SQLAlchemy and SQLObject, Storm also maps tables to classes, rows to instances and columns to attributes. Compared to the other two, Storm's table classes do not have to be subclasses of a special framework-specific superclass. In SQLAlchemy, every table class is a subclass of Similar to SQLAlchemy, Storm's Specific versions of Storm can be downloaded from the download page. In this article, the example code is written in Storm version 0.20. [python] The code above created an in-memory sqlite database and a store to reference that database object. A Storm store is similar to a SQLAlchemy DBSession object, both of which manage the life cycles of instance objects attached to them. For example, the following code creates a person and an address, and inserts both records into the database by flushing the store. [python] >>> store.execute("CREATE TABLE address " >>> person = Person() >>> print "%r, %r" % (person.id, person.name) To get or retrieve the inserted Person and Address objects, we call [python] >>> address = store.find(Address, Address.person == person).one() Django's ORMDjango is a free and open source web application framework whose ORM is built tightly into the system. After its initial release, Django becomes more and more popular due to its straightforward design and easy-to-use web-ready features. It was released under the BSD license in July 2005. Since Django's ORM is built tightly into the web framework, it's not recommended, although possible, to use its ORM in a standalone non-Django Python application. Django, one of the most popular Python web frameworks, has its own dedicated ORM. Compared to SQLAlchemy, Django's ORM is more geared towards direct SQL object manipulation where it exposes a plain and direct mapping between database tables and Python classes. [shell] You just installed Django's auth system, which means you don't have any superusers defined. Since we cannot execute Django's code without creating a project first, we created a Django project 'demo' in the previous shell and entered the Django shell to test our ORM example. [python] The code above declared two Python classes, [shell] To insert a person and an address into the database, we instantiate the corresponding objects and call the [python] To get or retrieve the person and address objects, we use the magical [python] >>> a = addresses[0] >>> print "%r, %r" % (a.id, a.address) 1, u'address' [/python] peeweepeewee is a small, expressive ORM. Compared to other ORMs, [shell] Installing collected packages: peewee changing mode of /Users/xiaonuogantan/python2-workspace/bin/pwiz.py to 755 To create a database model mapping, we implement a [python] To insert objects into the database, we instantiate the objects and call their [python] To get or retrieve the objects from the database, we [python] PonyORMPonyORM allows you to query the database using Python generators. These generators are translated into SQL and the results are automatically mapped into Python objects. Writing queries as Python generators makes it easy for programmers to quickly construct certain queries. For example, let's use PonyORM to query the previous [python] Now we have a SQLite database in memory and two tables mapped to the [python] The call [python] SQLAlchemySQLAlchemy is an open source SQL toolkit and ORM for the Python programming language released under the MIT license. It was released initially in February 2006 and written by Michael Bayer. It provides "a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language". It has adopted the data mapper pattern (like Hibernate in Java) rather than the active record pattern (like the one in Ruby on Rails). SQLAlchemy's unit-of-work principal makes it essential to confine all the database manipulation code to a specific database session that controls the life cycles of every object in that session. Similar to other ORMs, we start by defining subclasses of [python] Before we write any database code, we need to create an database engine for our db session. [python] Once we have created a database engine, we can proceed to create a database session and create tables for all the database classes previously defined as [python] Now the [python] To get or retrieve the database objects, we call [python] >>> print "%r, %r" % (p.id, p.name) Notice that so far we haven't committed any changes to the database yet so that the new person and address objects are not actually stored in the database yet. Calling [python] Comparison Between Python ORMsFor each Python ORM presented in this article, we are going to list their pros and cons here: SQLObjectPros:
Cons:
StormPros:
Cons:
Django's ORMPros:
Cons:
peeweePros:
Cons:
SQLAlchemyPros:
Cons:
PonyORMPros:
Cons:
Summary and TipsCompared to other ORMs, SQLAlchemy stands out in its focus on the unit-of-work concept which is prevalent whenever you write SQLAlchemy code. The DBSession concept might be hard to understand and use correctly initially, but later you will appreciate the additional complexity which reduces accidental database commit-timing-related bugs to almost zero. Dealing with multiple databases in SQLAlchemy can be tricky since each DB session is confined to one database connection. However, this kind of limitation is actually a good thing since it forces you to think hard about the interaction between multiple databases and make it easier to debug database interaction code. In the future articles, we are going to fully explore more advanced use cases of SQLAlchemy to truly grasp its immensely powerful APIs. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |