mapping objects

Cameron Simpson cs at zip.com.au
Sun Feb 2 01:58:09 EST 2014


On 01Feb2014 20:46, Rita <rmorgan466 at gmail.com> wrote:
> I want to learn more about ORMs so I stumbled upon, SqlAlchemy.
> 
> If i had a JSON document (or XML, CSV, etc.._) is it possible to convert it
> to a SQLAlchemy objects? I like the ability to query/filter (
> http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#common-filter-operators)
>  the
> data assuming I set up the proper schema.

Well, not as directly as you might hope. As I recall, SQLAlchemy
ORMs provide an easy way to make objects representing database
entities and whose methods automatically drive the necessary SQL
actions to manipulate them.

On that basis, you won't get anywhere without loading up the
JSON/XML/etc, parsing it for relevant information (trivial with
CSV, less so for structured data like JSON or XML), and storing it
in a database. To which you then point SQLAlchemy.

So you're already doing the fiddly bit just to get stuff into the database.

The neat filter operations you cite are actually done by special
methods on the objects representing tables and columns. For example,

  User.name == None

is done via the __eq__ method of "User.name". And they return
strings: bare SQL. The beauty of this is that one can write almost
idiomatic python, and SQLA will generate correct SQL in the right
dialect for the database backend, and with all the values correctly
escaped.

However, it does presume you _already_ have a backend that can be
queried with SQL.

> Also, is this a valid way to use an ORM suite?

Well, in principle sure. But SQLA won't do it for you directly. It really
is for data already stored in an SQL queriable database.

Your point about using SQLA's filter operations is right, _provided_
you have already loaded the original data into a database with the
right schema. If you've done that work, then SQLA may well serve
you well from that point on.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

We knew Army cadets were involved because they cut through two fences
to get to the goats, and 15 feet away there was an unlocked gate.
        - a director of sports information in the Navy, regarding the theft
          of some mascots from the Naval Academy by Army rivals



More information about the Python-list mailing list