After some discussion on #sqlalchemy, I solved this problem by moving
the declaration of the relationship outside of the class declaration
(see below).

Is this a good / common way of defining relationships?

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

    id = db.Column(db.Integer, primary_key=True)
    class_type = db.Column(db.String)
    class_id = db.Column(db.Integer)
    label = db.Column(db.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)


class Category(db.Model):
    __tablename__ = 'category'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    description = db.Column(db.String)
    order = db.Column(db.Integer)

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

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

Category.ui_metadata = db.relationship(UIMetadata,
primaryjoin=and_(Category.id==UIMetadata.class_id,
UIMetadata.class_type=="Category"),
foreign_keys=[UIMetadata.class_id])


Note: the code above is using Flask-SQLAlchemy, hence the db.
prefixes.



On May 10, 12:09 pm, nostradamnit <[email protected]> wrote:
> 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