thanks a lot.
i'm used to hand-coded sql.
I've already the exact class / mapper structure that you suggested -
i figured that out based on the docs.
I should have been a bit more clear on this point - I pretty much
understand the selecting and pulling data.
And I understand the 'simple' inserts and joining 2 objects together.
What I can't figure out though, is doing this sort of insert:
engine.begin()
myInstance = Instance()
Instance.name = 'random name'
Instance.type = ?
engine.commit()
Where Instance.type is an id foreign keyed from
Instance.instance_type_id -> InsanceType.id
To accomplish this would it just be
Instance.type_id = InstanceType.mapper.select_by(name= 'FOO')
Or are there other ways?
A lot of stuff i do is part-python and part-perl -- so I often store
about 200 id/name values that are really configuration items into the
database itself
For stuff like this, i'm wondering if pre-caching these pairs is the
way to go.
On Apr 25, 2006, at 1:37 PM, Gambit wrote:
Hey Jonathan,
Welcome to ORMs :P
I think your basic misunderstanding here is that with an ORM you're
going to
be mapping columns -- potentially brought together from different
tables --
into a single cohesive unit that represents a logical grouping,
rather then
collapsing everything into a bucket. Drawing from the examples in
the docs,
you might have a "Users" class in Python that is -- and this is the
cool part
-- automatically populated, committed, updated, deleted, etc. by
the ORM
engine, in this case SQLAlchemy.
So basically you don't deal with pre-caching your records anymore,
for a
couple good reasons, two of which being pre-caching can be
hideously expensive depending
on the size of the recordset, and second, it's often simpler to
request
specific results when needed.
Looking at your example code, I'd suspect something like the
following may be
what you're looking for:
class Instance(object):
pass
class InstanceType(object):
pass
Instance.mapper = mapper(Instance, instance)
InstanceType.mapper = mapper(InstanceType, instance_type)
# This could easily be rolled into the above Instance.mapper
declaration, but
# for transparency we'll keep it separate.
Instance.mapper.add_property('type', relation(InstanceType.mapper)
# Now that we have the class objects set up, we can access them
like:
myins = Instance.mapper.get(5) # Get the Instance with a
primary key of 5
print 'Instance number 5 is of type:', myins.type.name
# You could also search by instance name. This produces the
first Instance
# that matches the name 'FOO'.
myins = Instance.mapper.select_by(instance.c.name = 'FOO')[0]
There's lots of other ways to do these things, and the
documentation is fairly
extensive. There's also a introduction/tutorial, state unknown at:
http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/doc/
build/content/tutorial.txt
And, as always, irc://irc.freenode.net/#myghty where a few of us
hang out :)
//Jonathan Vanasco
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
| RoadSound.com / Indie-Rock.net
| Collaborative Online Management And Syndication Tools
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users