On Aug 19, 2:27 am, "King Simon-NFHD78" <[email protected]>
wrote:
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On Behalf Of darkblueB
> > Sent: 19 August 2009 02:58
> > To: sqlalchemy
> > Subject: [sqlalchemy] Re: new questions
>
> > Hi Simon
>
> > thanks for the reply.. Your second part is straightforward..
> > The first one, not so much..
>
> > I have gone back to a simple
> >     meta.bind = //my engine here
> >     meta.reflect()
>
> > I can see meta.tables, and meta.tables_sorted()
> > but how do I know what mapped objects exist?
>
> > (there should be about 12 tables, with a few one to many and one many
> > to many defined)
> > I feel like time is passing by, I would like to use ORM but this is
> > new to me
> > thanks much
> >   -Brian
>
> Ah - I see what you mean now. meta.reflect() will only create Table
> objects (part of the SQL expression language layer). It doesn't
> automatically create mapped classes for you. If you want to use the
> declarative system, you would need to create at least a basic class for
> each of your reflected tables.
>
> For example (untested):
>
> import sqlalchemy as sa
> from sqlalchemy.ext.declarative import declarative_base
>
> meta = sa.Metadata(bind='sqlite:///your.db')
> meta.reflect()
>
> Base = declarative_base(metadata=meta)
>
> class SomeObject(Base):
>     __table__ = meta.tables['some_table']
>
> class SomeOtherObject(Base):
>     __table__ = meta.tables['some_other_table']
>
> I suppose you could automate this by iterating over meta.tables and
> creating the classes in a loop. You would end up with something similar
> to SqlSoup (http://www.sqlalchemy.org/trac/wiki/SqlSoup). I suppose it
> should be possible to automatically create ORM-level relations by
> inspecting your foreign keys as well if you really wanted.
>


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


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to