attached is a script illustrating the usage of comparable_property, in roughly the same way you were using composite earlier.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.orm.interfaces import PropComparator
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite://', echo=True)

Base = declarative_base(bind=engine)

class MyComparator(PropComparator):
    def __eq__(self, other):
        return and_(User.id==other.id, User.name==other.name)

class Comp(object):
    def __init__(self, id, name):
        self.id = id
        self.name = name
    
    def __eq__(self, other):
        return self.id == other.id and self.name == other.name
        
class Address(Base):
    __tablename__ = 'address'

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    street = Column('street', CHAR)

class User(Base):
    __tablename__ = 'user'

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    name = Column('name', CHAR)
    house_address_id = Column('house_address', Integer, ForeignKey('address.id'))
    office_address_id = Column('office_address', Integer, ForeignKey('address.id'))
    house_address = relation(Address, primaryjoin=house_address_id==Address.id)
    office_address = relation(Address, primaryjoin=office_address_id==Address.id)
    
    @property
    def comp(self):
        return Comp(self.id, self.name)

    comp = comparable_property(MyComparator, comp)

Base.metadata.create_all()

session = sessionmaker()()
session.save(
    User(
        name='user1', 
        house_address=Address(street='somestreet'), 
        office_address=Address(street='someotherstreet')
    )
)

rows = session.query(User).filter(User.comp==Comp(1, "user1")).all()
assert rows[0].comp == Comp(1, "user1")





On Nov 4, 2008, at 7:29 PM, Ritesh Nadhani wrote:

Unfortunately, it still gives me an error.

http://paste.pocoo.org/show/90191

Did I miss something?

PS: I added the __get__ method just for the fun of it, I have no idea what it does. Looking at the docs: http://www.sqlalchemy.org/docs/04/sqlalchemy_orm_interfaces.html#docstrings_sqlalchemy.orm.interfaces_PropComparator , seems that I have to implement other methods but I am not sure which one.

Any help is appreciated.

On Mon, Oct 27, 2008 at 7:45 AM, Michael Bayer <[EMAIL PROTECTED]> wrote:

theres a "bug" in that the error message is misleading, but in fact a
composite property owns the columns within it which cannot be mapped
separately, so to make that "work" you'd need  to say:

class User(Base):

    __tablename__ = 'user'

    house_address_id = Column('house_address', Integer,
ForeignKey('address.id'))
    office_address_id = Column('office_address', Integer,
ForeignKey('address.id'))
    house_address = relation(Address,
primaryjoin=house_address_id==Address.id)
    office_address = relation(Address,
primaryjoin=office_address_id==Address.id)
    comp = composite(Comp, Column('id', Integer, primary_key=True,
autoincrement=True), Column('name', CHAR))

but the way you're using Comp isn't going to work in any case;  you're
actually looking for comparable_property() here:

class MyComparator(sqlalchemy.orm.interfaces.PropComparator):
    def __eq__(self, other):
        return self.comp == other.comp

class User(Base):

    __tablename__ = 'user'

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    name = Column('name', CHAR)
    house_address_id = Column('house_address', Integer,
ForeignKey('address.id'))
    office_address_id = Column('office_address', Integer,
ForeignKey('address.id'))
    house_address = relation(Address,
primaryjoin=house_address_id==Address.id)
    office_address = relation(Address,
primaryjoin=office_address_id==Address.id)

    @property
    def comp(self):
        return self.id + self.name

    comp = comparable_property(MyComparator)


On Oct 27, 2008, at 9:22 AM, riteshn wrote:

>
> Hello all
>
> New to SQLAlchemy and ORM and loving it. I am trying to use the
> declarative base extension with composite column.
>
> I have two very simple tables - user and address.
>
> My code at: http://python.pastebin.com/m6e032164 works without any
> problem.
>
> I am trying to put the same thing using declarative base:
> http://python.pastebin.com/m1a05e5c0 and it throws me the error.
>
> Any ideas?
>
> >






--
Ritesh
http://www.riteshn.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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