I've recently started using SQLAlchemy and is a newb. I have good SQL
knowledge though. My first project with SQLAlchemy is a big project
where I integrate SQLAlchemy to a Plone application. So far it has
been working great for queries from a single table however queries
with joins in two or more tables is way more difficult.

I've tried to join two tables with two different queries without
success

The query:
objects = db.session.query(Sak).join(Prosjekt,
Sak.prosjektid).filter(Prosjekt.kundeid==15000032).all()
returns this error:
InvalidRequestError: Mapper 'Mapper|Sak|sak' has no property '<class
'Products.TikkTakk.model.Prosjekt.Prosjekt'>'

The query:
objects = db.session.query(Sak).from_statement("SELECT s.saksnr,
s.tittel FROM sak s INNER JOIN prosjekt p ON s.prosjektid =
p.prosjektid WHERE p.kundeid = 15000032).all()
returns this error:
NoSuchColumnError: "Could not locate column in row for column
'sak.prosjektid'"

I have some trouble figuring out what I'm doing wrong. Especially the
NoSuchColumnError is frustrating as the column prosjektid exists in
the table sak, it's not a typo either ;)

The mapping looks like this:
mappers['prosjekt'] = mapper(Prosjekt, tables['prosjekt'],
            properties = {
            'sak': relation(Sak)
            })
mappers['sak'] = mapper(Sak, tables['sak'],
            properties = {
            'prosjekt': relation(Prosjekt),
            })

And the models like this:
class Prosjekt(Entity):
    """ Prosjekt entity map
    """

    prosjektid = Field(mysql.MSInteger, primary_key=True)
    p_prosjektid = ManyToOne('Prosjekt')
    sak = OneToMany('Sak')

    using_options(shortnames=True)

    def __init__(self, data):
        """ Objects are initialized using form data
        """

    def __repr__(self):
        return '<Prosjekt (%d)>' % (self.prosjektid)

class Sak(Entity):
    """ Sak entity map
    """

    prosjektid = ManyToOne('Prosjekt', primary_key=True)
    saksnr = Field(mysql.MSInteger, primary_key=True)

    using_options(shortnames=True)

    def __init__(self, data):
        """ Objects are initialized using form data
        """

    def __repr__(self):
        return '<Sak (%d)>' % (
            self.prosjektid,
            self.saksnr,
            )

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