I would just add that I often do::
City = db.define_table('City', Field('Name', 'string'), Field('Country',
db.Country))
paris = City.insert(Name='Paris', Country=france)
apart for the ".insert" this is the same as what Arnon suggests. The lack
of ".insert" in ORMs:
paris = City(Name='Paris', Country=france)
make the time when the data is actually stored in the database undefined
(deferred). This is why Django and Active Records need a City.save(). The
developer loses controls about it and then the internal consistency problem
arises. When some people like about ORMs is also their
most criticized feature: You do not know when they do DB IO.
In web2py DAL we managed to achieve a notation that is very similar to ORMs
while preserving the 1-to-1 mapping from API and SQL. We may extend this
but I would not want to lose this explicit mapping. In fact in web2py DAL
insert, delete, update, selet are the only methods (apart for auxiliar ones
like record_update, record_update, tables, files). Almost everything else
is achieved by operator overloading.
On Thursday, 2 May 2013 18:14:17 UTC-5, Anthony wrote:
>
> db.define_table('Continent', Field('Name', 'string'))
>
> db.define_table('Country', Field('Name', 'string'), Field('Continent',
>> db.Continent))
>> db.define_table('City', Field('Name', 'string'), Field('Country', db.
>> Country))
>>
>> Using an ORM, you could do something like:
>> >>> Country(Name='France').City.list
>> [<City Name:Paris>, <City Name:Nice>]
>>
>> Using the DAL, the best you might get is:
>> >>> [city for city in db.City.Country.select() if city.Country.Name ==
>> ''France']
>> [<Row Name:Paris>, <Row Name:Nice>]
>>
>
> Actually, you can do this with the DAL:
> >>> db.Country(name='France').City.select()
>
> Almost identical to your ORM code (not that I think it needs to be similar
> looking code to be useful).
>
> >>> europe = Continent(Name='Europe')
>> >>> france = Country(Name='France', Continent=europe)
>> >>> paris = City(Name='Paris', Country=france)
>> >>> nice = City(Name='Nice', Country=france)
>> >>> europe.Country(Name='France').City.list
>> [<City Name:Paris>, <City Name:Nice>]
>>
>
> In the DAL, you can do (I think I got this right):
>
> >>> europe = db.Continent.insert(Name='Europe')
> >>> france = db.Country.insert(Name='France', Continent=europe)
> >>> paris = db.City.insert(Name='Paris', Country=france)
> >>> nice = db.City.insert(Name='Nice', Country=france)
> >>> db.Country(france).City.select()
> [<Row Name:Paris>, <Row Name:Nice>]
>
> Of course, you don't get the "is" equivalencies, but your example doesn't
> actually require that.
>
> Anthony
>
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.