Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Talk about making a mountain out of a molehill. At my company, we don't try to turn database rows into objects. Here is how it works:

   * we use Python+MySQL
   * each table has an associated class, a CRUD-API if you will
   * a cursor object accesses a table like this:
    cursor.TableName.select_by_ids(low, high)
   * removing means set time_removed to the current timestamp
   * rows are returned as lists of dicts
So the solution to the object-relational mapping problem is: for each table, there is a class that manages access to it, and a row is represented by a a dict.


How do you map the types if the rows are returned as dicts? Doesn't that become an application responsibility? By using an ORM you would not need to build that functionality, i.e. reinvent the wheel.

Did you consider using SQLObject or SQLAlchemy? If so, I'd be curious to know why you decided against using them?


There are lots of ways to do this. A most common approach is to have each of those dicts be a first class object in python/blub, and therefore you know the class of the object. Customer.find() returns a customer which contains your dict. No magic going on here. You are not reinventing too much by deciding to use lightweight wrappers and eschew automated SQL generation or ORMS and related DSLs.


You mentioned a "common approach". Is this the approach that you use or have you found a better way? The reason I ask is I don't quite understand how your description above correlates to an ORM type-mapping system. Customer.find() returns a customer which contains your dict. I don't know python but I assume a dict is a type-less hashmap. So if your customer has a field called 'updated_at', wouldn't you need to write a function to convert that type appropriately for the application since the application doesn't know whether it's a Date, Datetime or Timestamp?


Yes, dict is a hash map. A python dict is not "typeless", it contains whatever python objects you put in it.

Most client drivers/libs for RDBs have basic type conversions for each language. You don't always need an ORM for this. If you have a ruby or python Time object, the low level db lib generally will convert it to/from the RDB format.

I've used both simple and complex mapping methods. At the moment, I'm using mongodb with very simple first class object wrappers around the hash/dict. This is appropriate for this new app.

For an app I already have in production, I use postgresql and ruby's datamapper. This limits me to knowing that adding some features requires more work so those features keep getting pushed off the plate. The reason I use postgres for this app is because my users expect RDB ACID properties. For the new app I'm working on, not so much.


Most client drivers/libs for RDBs have basic type conversions for each language. You don't always need an ORM for this. If you have a ruby or python Time object, the low level db lib generally will convert it to/from the RDB format.

Didn't know that. If that's correct, then the low-level db lib is an ORM in Python. In which case SQLObject/SQLAlchemy probably just harnesses those features and adds some additional cream on top.


Most all low level rdb libs provide basic type conversion from allowed types in the db to type fitting the language you are using. From these libs you send SQL (any SQL you care to send) and you get returns of a "row" or a cursor usually.

An ORM adds stuff on top of the driver. General these frameworks provide relationship management, connection pooling, caching, and config management.


According to the Wikipedia definition of ORM, it's primarly about the mapping of types:

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages.

http://en.wikipedia.org/wiki/Object-relational_mapping


Thats a pretty decent definition. I think their phrase "incompatible type systems" is meant for you to stress the word systems. The word type is not needed. Not to be confused with "type of an object, i.e. its class".

If you really want to know what's in various lower level drivers, as opposed to ORM frameworks (which vary quite a bit themselves), just go crack open some code and look at the APIs for yourself. You can go pretty far back to MS ODBC libs or even vendor dependent Oracle libs from mid to late 80s and see they were used quite effectively on their own for many years. JDBC was fairly straightforward derived from these predecessors.


Why reinvent the wheel? An ORM will do all of that for you.


yes, an ORM will do all that and then some. Sometimes you don't want the extra stuff. I'm not knocking ORMs. They serve their purpose. Sometimes all I need though is a simple lower level driver.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: