Hi again,

I thought I should reply and update on what the issue was.

It might be possible that __declare_last__() isn't appropriate here, can 
> you try just saying "Context.link = column_property()"... right after 
> you declare Context? 
>

This was a good advice and ultimately led me to the solution. Since 
__declare_last__ runs for all the subclasses as well I ended up with the 
link attribute on both Context and the subclasses. Changing this to limit 
it only on Context solved the issue for me.

Thanks!
Mattias L


On Monday, November 2, 2015 at 3:16:07 PM UTC+1, Mattias Lagergren wrote:
>
> Hi,
>
> I'm trying to use load_only and joinedload on a relationship 
> model.Asset.parent. The parent relation is polymorphic and can be either 
> Task or Project with the common Base class called Context.
>
>         import sqlalchemy.orm
>         import sqlalchemy.inspection
>
>         entity = model.session.query(
>             model.Asset
>         ).options(
>             sqlalchemy.orm.joinedload('parent').load_only(
>                 'context_type', 'name', 'link'
>             )
>         ).first()
>
>         state = sqlalchemy.inspection.inspect(entity.parent)
>         for attribute in state.attrs:
>             is_loaded = (
>                 attribute.loaded_value is not
>                 sqlalchemy.orm.base.NO_VALUE
>             )
>             if is_loaded:
>                 print attribute.key
>
> # Output:
> id
> taskid
> name
> context_type
>
>
> The id, name and context_type is from Context. And taskid is primary key 
> on the taskid and is a foreignkey to the context.id. As you can see 
> "name" loads fine but "link" attribute is not loaded. The "link" column is 
> added as a column_property to Context using a __declare_last__. 
>
> These are simplified versions of the classes:
>
>
> class Context(Base):
>     '''Represent a context.'''
>     context_type = Column(Unicode(32), nullable=False)
>     
>     __mapper_args__ = {
>         'polymorphic_on': context_type,
>         'polymorphic_identity': 'context'
>     }
>
>     name = Column(Unicode(255), default=u'', nullable=False)
>
>     @declared_attr
>     def id(cls):
>         return Column(CHAR(36), primary_key=True, default=lambda: str(uuid
> ()))
>
>     @classmethod
>     def __declare_last__(cls):
>         '''Return link expression query.'''
>         
>         ...
>
>         cls.link = column_property(
>             sqlalchemy.type_coerce(
>                 query, LinkTypeDecorator
>             ).label('link')
>         )
>
> class Task(Context):
>     '''Represent a task.'''
>
>     taskid = Column(
>         types.CHAR(36),
>         ForeignKey('context.id'),
>         primary_key=True
>     )
>
>     __mapper_args__ = {
>         'polymorphic_identity': 'task'
>     }
>
>
> class Asset(Base):
>     '''Represent an Asset.'''
>
>     context_id = sqlalchemy.Column(
>         sqlalchemy.CHAR(36), sqlalchemy.ForeignKey('context.id')
>     )
>
>     parent = relationship('Context', backref=backref('assets'))
>
>
> Can you see if I'm doing something wrong?
>

-- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to