On Jun 10, 2013, at 2:00 PM, Daniel Grace <[email protected]> wrote:

> I'm converting some existing code from reflecting tables in the database to 
> actually defining all of the columns.  One of my primary reasons for doing 
> this is being able to apply mixins and such to table definitions -- for 
> instance, there's numerous lookup tables which all have "id" and "name" 
> columns.
> 
> My problems arise when trying to declare an order_by that references columns 
> defined in a superclass -- e.g: wanting to do something like this:
> 
> class LookupTableMixin():
>     def __str__(self):
>         return self.name
> 
> class LookupTable(Model, LookupTableMixin):
>     __abstract__ = True
>     id = Column(INTEGER(), primary_key=True)
>     name = Column(TEXT(), nullable=False)
>     
> class SortedLookupTable(LookupTable):
>     __abstract__ = True
>     __mapper_args__ = { 'order_by': [ name ] }
> 
> 
> I'm trying to determine the correct way to do this:


> 4. Simply redefining 'id' and 'name' (e.g. copy-pasting the column 
> definitions from LookupTable to SortedLookupTable) ultimately causes the 
> following:
> 
> CompileError("Cannot compile Column object until its 'name' is assigned.",)
> 
> Oddly enough, this only happens if __mapper_args__ is also present (and 
> ordering by the column)
> 
> 5. __mapper_args__ does not appear to work with declared_attr, such that:
> 
>     @declared_attr
>     def __mapper_args__(cls):
>         return { 'order_by': [ cls.name ] }
> 
> causes the same error as above.

Using 0.8 you can pull the actual copied column "name" from the __table__:

class SortedLookupTable(LookupTable):
    __abstract__ = True

    @declared_attr
    def __mapper_args__(cls):
        return {'order_by': [cls.__table__.c.name]}

"cls.name" is the un-copied Column object still associated with the LookupTable 
abstract class, ready for use on other mapped classes.    This pattern is 
discussed in this section: 
http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html#resolving-column-conflicts
 .

Also as far as the "order_by" mapper argument, there's no plans to remove it, 
but it's a legacy argument that doesn't work well/at all with many queries 
(such as queries against multiple entities, queries against individual columns).

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to