Python’s SQLAlchemy vs Other ORMs[转发 2]Storm
Storm
Storm 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 sqlalchemy.ext.declarative.declarative_bas
. In SQLObject, every table class is a subclass of sqlobject.SQLObject
.
Similar to SQLAlchemy, Storm's Store
object acts as a surrogate to the backend database, where all the operations are cached in-memory and committed
into the database once the method commit is called on the store. Each store holds its own set of mapped Python database objects, just like a SQLAlchemy session holding different sets of Python objects.
Specific versions of Storm can be downloaded from the download page. In this article, the example code is written in Storm version 0.20.
1 >>> from storm.locals import Int, Reference, Unicode, create_database, Store 2 >>> 3 >>> 4 >>> db = create_database('sqlite:') 5 >>> store = Store(db) 6 >>> 7 >>> 8 >>> class Person(object): 9 ... __storm_table__ = 'person' 10 ... id = Int(primary=True) 11 ... name = Unicode() 12 ... 13 >>> 14 >>> class Address(object): 15 ... __storm_table__ = 'address' 16 ... id = Int(primary=True) 17 ... address = Unicode() 18 ... person_id = Int() 19 ... person = Reference(person_id, Person.id) 20 ...
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.
1 >>> store.execute("CREATE TABLE person " 2 ... "(id INTEGER PRIMARY KEY, name VARCHAR)") 3 4 >>> store.execute("CREATE TABLE address " 5 ... "(id INTEGER PRIMARY KEY, address VARCHAR, person_id INTEGER, " 6 ... " FOREIGN KEY(person_id) REFERENCES person(id))") 7 8 >>> person = Person() 9 >>> person.name = u'person' 10 >>> print person 11 12 >>> print "%r, %r" % (person.id, person.name) 13 None, u'person' # Notice that person.id is None since the Person instance is not attached to a valid database store yet. 14 >>> store.add(person) 15 >>> 16 >>> print "%r, %r" % (person.id, person.name) 17 None, u'person' # Since the store hasn't flushed the Person instance into the sqlite database yet, person.id is still None. 18 >>> store.flush() 19 >>> print "%r, %r" % (person.id, person.name) 20 1, u'person' # Now the store has flushed the Person instance, we got an id value for person. 21 >>> address = Address() 22 >>> address.person = person 23 >>> address.address = 'address' 24 >>> print "%r, %r, %r" % (address.id, address.person, address.address) 25 None, , 'address' 26 >>> address.person == person 27 True 28 >>> store.add(address) 29 >>> 30 >>> store.flush() 31 >>> print "%r, %r, %r" % (address.id, address.person, address.address) 32 1, , 'address'
To get or retrieve the inserted Person and Address objects, we call store.find()
to find them:
1 >>> person = store.find(Person, Person.name == u'person').one() 2 >>> print "%r, %r" % (person.id, person.name) 3 1, u'person' 4 >>> store.find(Address, Address.person == person).one() 5 6 >>> address = store.find(Address, Address.person == person).one() 7 >>> print "%r, %r" % (address.id, address.address) 8 1, u'address'