I'm trying to implement an alternative join relationship pretty much
exactly like the boston_addresses example in the documentation, but
I'm running into a 'Could not determine relationship direction...'
error. I've included the code example. I've tried every possible
variation I could think of :(

Here's the basic SQL implementation I'm looking to accomplish.

select c.id as cat_id, c.name, ui.label
from category c join ui_metadata ui
        on c.id = ui.class_id and ui.class_type = 'Category'


Once I figure out how to formulate the relationship, there will be
other objects like Category that will have similarily related
ui_metadata.

Thanks in advance for you assistance,
Sam


The example code (I didn't have an attach option):

from sqlalchemy import create_engine, and_
from sqlalchemy import Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref

Base = declarative_base()
metadata = MetaData()


class Category(Base):
    __tablename__ = 'category'

    id = Column(Integer, primary_key=True)
    name = Column(String)

    #features = relationship('Feature')
    ui_metadata = relationship('UIMetadata',
primaryjoin=and_('category.c.id==ui_metadata.c.class_id',
'ui_metadata.c.class_type=="Category"'))


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

    def __repr__(self):
        return '<Category %r>' % (self.name)

class UIMetadata(Base):
    """ Class to allow UI metadata to be associated with almost any
other class"""
    __tablename__ = 'ui_metadata'

    id = Column(Integer, primary_key=True)
    class_type = Column(String)
    class_id = Column(Integer)
    label = Column(String)

    def __init__(self, class_type, class_id, label):
        self.class_type = class_type
        self.class_id = class_id
        self.label = label

    def __repr__(self):
        return '<UIMetadata (%s) %r(id=%s)>' % (self.id,
self.class_type, self.class_id)



def start_session(db_path = None):
    if not db_path:
        db_path = 'test.db'
    engine = create_engine('sqlite:///%s' % db_path, echo=True)
    Session = sessionmaker(bind=engine)
    session = Session()
    Base.metadata.create_all(engine)

    return session

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