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:

1. As written, it doesn't work since 'name' is not defined yet.
2. Using "name" in quotes "works", up until the point where the the 
automatic sort causes a conflict since it refers to any column named "name" 
(ORDER BY name, rather than ORDER BY __tablename__.name)
3. No form of callable that I've tried seems to work 
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.

What should I be doing here to get this to work?

Thanks in advance,

-- Daniel

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
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