Hello!

I have two classes that are mapped to tables from different databases.
The classes are defined with declarative_base. I can query each class
individually, but a joined query fails. Here is an example:


from sqlalchemy.orm import
sessionmaker
import sqlalchemy as
sa
from sqlalchemy.ext.declarative import
declarative_base

people_engine =
sa.create_engine('sqlite://')
Base1 =
declarative_base(bind=people_engine)
class
People(Base1):
    __tablename__ = 'People'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String)

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return self.name
Base1.metadata.create_all()

email_engine = sa.create_engine('sqlite://')
Base2 = declarative_base(bind=email_engine)
class Email(Base2):
    __tablename__ = 'Email'
    id = sa.Column(sa.Integer, primary_key=True)
    email = sa.Column(sa.String)
    people_id = sa.Column(sa.Integer, sa.ForeignKey(People.id))
    People = sa.orm.relationship(People, backref='Emails')

    def __init__(self, email):
        self.email = email

    def __repr__(self):
        return self.email
Base2.metadata.create_all()

session = sessionmaker(binds={Email:email_engine,
People:people_engine})()

# add some data
p1 = People('Joe')
p1.Emails = [Email('j...@gmail.com'), Email('j...@hotmail.com')]
session.add(p1)

p2 = People('Mary')
p2.Emails = [Email('m...@gmail.com'), Email('m...@yahoo.com')]
session.add(p2)

p3 = People('Pat')
p3.Emails = [Email('p...@hotmail.com')]
session.add(p3)

session.commit()

# try some queries
# these queries run fine
print session.query(Email).filter(Email.email.like('%gmail%')).all()
print session.query(People).filter(People.name.like('%a%')).all()

# this query fails with the error message:
#   sqlalchemy.exc.OperationalError: (OperationalError) no such table:
People u'SELECT "Email".id
#   AS "Email_id",  "Email".email AS "Email_email", "Email".people_id
AS "Email_people_id"
#   FROM "Email" JOIN "People" ON "People".id = "Email".people_id
\nWHERE "People".name = ?' ('Mary',)
print
session.query(Email).join(People).filter(People.name=='Mary').all()

When the same DB is used for the two classes, everything runs ok,
including the joined query. I believe this is an example of "vertical
partitioning". Is there anything I'm missing?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to