Hey,

Just started using 0.2.1, it's pretty neat, though converting from
objectstore to session was a bit of a mindbender; thanks to rmunn for the
tutorial though, it filled in the major points so I was able to fix it good.

Still not sure if I like the explicit session better than the implicit
objectstore, but we'll see how it goes.

Anyway, I wanted to ask how one would go about doing a different kind of
many-to-one mapping: mapping two tables to a single data model.

Consider the following pseudopython:

account = Table('account',
        Column('id', Integer, primary_key=True),

        Column('email_address', String),
        Column('password', String),
        )

person = Table('person',
        Column('id', Integer, primary_key=True),
        Column('account_id', Integer, ForeignKey('account.id'),

        Column('handle', String),
        Column('firstname', String),
        Column('lastname', String),
        )

class Person(object):
        def __init__(self, handle=None, firstname=None, lastname=None,
email_address=None, password=None):
                self.handle = handle
                self.firstname = firstname
                self.lastname = lastname
                self.email_address = email_address
                self.password = password

So, I want to map an instance of Person onto both tables, so that part of
the information is stored in 'account', and some in 'person'.  Obviously there
will be cases where rows in 'account' don't map to rows in 'person', so we
want the 'person.id' primary key to be the id for the mapper; but how to I
tell the mapper that the fields should map to different tables?  My naive
attempt was this:

mapper(Person, join(account, person), properties = {
        'id' : person.c.id,
        'handle': person.c.handle,
        'firstname': person.c.firstname,
        'lastname': person.c.lastname,
        'email_address': account.c.email_address,
        'password': account.c.password,
        })

but with echo=True in the engine, I'm seeing that the account primary key is
not being set in the foreign key column of person, and so when one tries to
retrieve the object from the session, it returns None.

Obviously I'm doing somethign wrong, because I'd expect SA to just handle
this case easily, (though it's never mentioned in the docs ;-), it seems
like an obvious thing to want to do.

Any ideas?  Thanks in advance.


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to