Yeah sorry I missed that. conversion is an attribute on Measured_Source.
So the intent is that a Production_Load is a Load with its own additional
attributes over Load as well as a constraint that its source is a
Measured_Source which has its own attribute extensions over Source. One of
the goals here is to add that constraint enforcement. I was able to make it
work with the following hybrid_method and hybrid_method.expression, but the
isinstance(Production_Load.source, Measured_Source) enforcement is missing.
Eric
{{{
class Production_Load(Load):
__tablename__ = 'production_load'
__mapper_args__ = { 'polymorphic_identity':'production_load' }
id = Column(Integer, ForeignKey('load.id'), primary_key=True)
top = Column(Numeric, nullable=False)
bottom = Column(Numeric, nullable=False)
@hybrid_method
def delta(self):
return (self.top-self.bottom)*self.source.conversion if self.source
else None
@gdelta.expression
def delta(self):
# not sure about the performance here
return
(self.top-self.bottom)*select([Measured_Source.conversion]).where(Measured_Source.id==self.source_id).label('delta')
}}}
On Wednesday, February 12, 2014 7:17:08 AM UTC-7, Michael Bayer wrote:
>
>
> On Feb 11, 2014, at 9:38 PM, Eric Atkin <[email protected] <javascript:>>
> wrote:
>
> > Hi,
> > I want to override a relationship in a subclass to relate to a subclass
> of the base attributes' related class. Perhaps an example of how I thought
> it should work:
> >
> > {{{
> > class Load(Base):
> > __tablename__ = 'load'
> > __mapper_args__ = {
> > 'polymorphic_identity':'load',
> > 'polymorphic_on':'polymorphic_type',
> > }
> > id = Column(Integer, primary_key=True)
> > polymorphic_type = Column(Text, nullable=False)
> > source_id = Column(Integer, ForeignKey('source.id'))
> > source = relationship('Source')
> >
> > class Production_Load(Load):
> > __tablename__ = 'production_load'
> > __mapper_args__ = { 'polymorphic_identity':'production_load' }
> > id = Column(Integer, ForeignKey('load.id'), primary_key=True)
> > source_id = Column(Integer, ForeignKey('measured_source.id'))
> > source = relationship('Measured_Source')
> >
> > class Source(Base):
> > __tablename__ = 'source'
> > __mapper_args__ = {
> > 'polymorphic_identity':'source',
> > 'polymorphic_on':'polymorphic_type',
> > }
> > id = Column(Integer, primary_key=True)
> > polymorphic_type = Column(Text, nullable=False)
> >
> > class Measured_Source(Source):
> > __tablename__ = 'measured_source'
> > __mapper_args__ = { 'polymorphic_identity':'measured_source' }
> >
> > id = Column(Integer, ForeignKey('source.id'), primary_key=True)
> > }}}
> >
> > As you can see, we have Load.source -> Source and I want
> Production_Load.source -> Measured_Source, but when I import the models, I
> get the following warning:
> >
> > {...}/env/lib/python2.7/site-packages/sqlalchemy/orm/properties.py:1028:
> SAWarning: Warning: relationship 'source' on mapper
> 'Mapper|Production_Load|production_load' supersedes the same relationship
> on inherited mapper 'Mapper|Load|load'; this can cause dependency issues
> during flush
> >
> > and when I try to use Production_Load.source (class level attr) in a
> query, I get the following error:
> >
> > AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator'
> object associated with Production_Load.source has an attribute 'conversion'
> >
> > Is such a thing possible, even with a re-factor of the models?
>
> “conversion” sounds like an attribute name on your end, but generally
> being able to supersede a relationship like that when the inheritance is
> not “concrete” is not supported. you’d need to name it to something else.
>
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.