Is is possible to set the polymorphic_on attribute on an object that
is not directly tied to a db table, but has access to the db attribute
via delegation?

I have a generic Product class that processes an XML to obtain its
generic attributes (uuid, type, etc). Afterwards, the product is
encapsulated within one of several proxy objects that perform
additional processing based on the enclosed product type. Each of the
various proxies are derived from a BaseProxy and multiple types can
use the same proxy. Due to this, the BaseProxy has a poly_type to
specify which of the proxies was used for additional processing. This
is a slimmed down version of what I'm trying to do:

Base = declarative_base()

class Product(Base):
    __tablename__ = 'products'

    id = Column(Integer, Sequence('id_seq'), primary_key=True)
    type = Column(String(16))

class BaseProxy(Base):
    __tablename__ = 'products'
    __table_args__ = {'extend_existing': True}

    poly_type = Column(String(16))

    __mapper_args__ = {'polymorphic_on': poly_type}

    product = relationship(Product, uselist=False)

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

    def __getattr__(self, attrib):
        return getattr(self.product, attrib)

class HardwareProduct(BaseProxy, Base):
    __tablename__ = 'hardware'
    __mapper_args__ = {'polymorphic_identity': 'hardware'}

    id = Column(Integer, ForeignKey('products.id'), primary_key=True)
    serial = Column(String(16))

class SoftwareProduct(BaseProxy, Base):
    __tablename__ = 'software'
    __mapper_args__ = {'polymorphic_identity': 'software'}

    id = Column(Integer, ForeignKey('products.id'), primary_key=True)
    product_key = Column(String(16))

However:

>>> p = Product()
sqlalchemy.exc.ArgumentError: Could not determine join condition
between parent/child tables on relationship ProductProxy.product.
Specify a 'primaryjoin' expression.  If 'secondary' is present,
'secondaryjoin' is needed as well.
>>>

While not directly related to SA, the reason that I didnt have
Hardware and SoftwareProduct inherit directly from Product was because
the product's type wasn't known until after product creation and, due
to the number of products created, I didn't want to waste the time
recreating the underlying product again and again. However, I'm
willing to change it if there is a better approach. Product creation
looks something like:

p = Product('product.xml')
p = select_proxy_based_on_type(p.type)(p)

I realize this is a drawn out question, but any help is appreciated.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to