Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy identity map question

The identity map and unit of work patterns are part of the reasons sqlalchemy is much more attractive than django.db. However, I am not sure how the identity map would work, or if it works when an application is configured as wsgi and the orm is accessed directly through api calls, instead of a shared service. I would imagine that apache would create a new thread with its own python instance for each request. Each instance would therefore have their own instance of the sqlalchemy classes and not be able to make use of the identity map. Is this correct?

like image 670
vikash dat Avatar asked May 03 '11 12:05

vikash dat


People also ask

What is identity map in SQLAlchemy?

identity map. A mapping between Python objects and their database identities. The identity map is a collection that's associated with an ORM Session object, and maintains a single instance of every database object keyed to its identity.

How do you define a composite primary key in SQLAlchemy?

To create a composite primary key, set primary_key to True on each column involved in the key. A boolean argument when set to False adds NOT NULL constraint while creating a column. Its default value is True .

Does SQLAlchemy require primary key?

¶ The SQLAlchemy ORM, in order to map to a particular table, needs there to be at least one column denoted as a primary key column; multiple-column, i.e. composite, primary keys are of course entirely feasible as well.


1 Answers

I think you misunderstood the identity map pattern.

From : http://martinfowler.com/eaaCatalog/identityMap.html

An Identity Map keeps a record of all objects that have been read from the database in a single business transaction.

Records are kept in the identity map for a single business transaction. This means that no matter how your web server is configured, you probably will not hold them for longer than a request (or store them in the session).

Normally, you will not have many users taking part in a single business transation. Anyway, you probably don't want your users to share objects, as they might end up doing things that are contradictory.

like image 194
Martin Avatar answered Oct 03 '22 14:10

Martin