darkblueB wrote:
>
> ok, I have done this
> I have an object def and a __table__ for all of the "main actors" in
> my setup
> I defined an __init__() for one of them
> I use the declarative base
>
> when the objects get created, they seem to have a __mapper__ in them
> already
> (the "primary mapper")
>
> but when I do simple queries that would draw upon a foreign key
> constraint present in the DB
> I dont get any lookups
>
> for example, (attribute? - I dont know all the terms here)
> //meta is setup already, sqlite
> // read in an existing DB
> cBase = declarative_base(metadata=meta)
>
> class saProject(cBase):
> __table__ = meta.tables['bt_projects']
>
> def __init__(self, inName, inFacilName, inImpModifier="",
> inProbModifier="", inIsInviteOnly=0, inAllowWt=0, inAllowEvtSummary=1,
> inAllowAssSum=1, inShowEvtRateAuth=1, inShowEvtRateCom=1 ):
> self.name = inName
> self.facilitator = inFacilName
> self.impact_mod = inImpModifier
> self.prob_mod = inProbModifier
> .....
>
> so now, I read one project in to tProj
> tProj.name => 'FBLS'
>
> but
> tProj.impact_mod => Decimal("0")
> *not* a lookup with the foreign key
>
> here is the relevant SQL CREATE DB
>
> CREATE TABLE 'bt_projects' (
> name TEXT UNIQUE,
> facilitator TEXT,
> active NUMERIC DEFAULT 1,
> impact_mod NUMERIC DEFAULT 0,
> prob_mod NUMERIC DEFAULT 0,
> ....
> key INTEGER NOT NULL PRIMARY KEY,
> FOREIGN KEY (impact_mod) REFERENCES bt_evt_prob_modifiers(key),
> FOREIGN KEY (prob_mod) REFERENCES bt_evt_impact_modifiers(key)
> );
>
> so, I have to make an explicit mapper, too?
> perhaps I am missing something
> -Brian
>
The "lookup with foreign key" that you are asking for is what SQLALchemy
calls a "relation" (See
http://www.sqlalchemy.org/docs/05/ormtutorial.html#building-a-relation).
You have to create them explicitly, and they can't have the same name as
the foreign key itself. When the foreign keys are simple, SA can
normally figure out the join conditions by itself.
For example, to get the behaviour your were expected, your class
definitions should look something like this:
import sqlalchemy.orm as saorm
class saEvtProbModifiers(cBase):
__table__ = meta.tables['bt_evt_prob_modifiers']
class saEvtImpactModifiers(cBase):
__table__ = meta.tables['bt_evt_impact_modifiers']
class saProject(cBase):
__table__ = meta.tables['bt_projects']
prob_modifier = saorm.relation(saEvtProbModifiers)
impact_modifier = saorm.relation(saEvtImpactModifiers)
Now when you access tProj.impact_modifier, you should get an instance of
the saEvtImpactModifiers class.
The relation function gies you lots of other options. You can configure
a 'back reference' (so you could automatically add a 'projects' property
to saEvtImpactModifiers and saEvtProbModifiers for example). You can
also explicitly define the join conditions if SA is unable to work them
out.
It's worth working through the ORM tutorial
(http://www.sqlalchemy.org/docs/05/ormtutorial.html) which covers all of
this.
Hope that helps,
Simon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---