Hello,
I have a class that stores files in a LarbeBinary attribute, such as:


class CommonBase(object):
    # Lets declare all the columns that are common to all
    # tables in our database.
    id = Column(Integer, primary_key=True)

...

Base = declarative_base(cls=CommonBase)

class DocumentBase(Base):
    __abstract__ = True

    filename = Column(String)

    @declared_attr
    def file_object(cls):
        return deferred(Column('file_object', LargeBinary))

This all works well but when I try to get the size of the stored file using 
the python len() function on the file_object attribute above, it takes a 
while because, to the best of my understanding, the binary object is first 
loaded into memory, and then len() computes its size.

I would like to make this more efficient by adding an attribute to the 
DocumentBase class above, which simply does a SQL query to get the size of 
the file_object at the DB level, thinking that this will not require to 
load the binary object into memory to compute its size.  Of course, this 
behavior could be done by adding an extra field (such as file_size) to my 
DB tables with binary object, which would be set when the binary object is 
stored in the database, but I thought that it would be more elegant if the 
DB didn't have that extra column just to keep track of the binary object 
size.


After reading and searching the web, I think one way to do this is using a 
column_property attribute in my DocumentBase class, such as:

    @declared_attr
    def file_size(cls):
        return 
column_property(select([func.length(cls.file_object)]).where(cls.id == id))

But the above is not working for me, and gives me the following error:

 File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", 
line 388, in visit_column
    raise exc.CompileError("Cannot compile Column object until "
CompileError: Cannot compile Column object until it's 'name' is assigned.

I'm sure I'm missing something very basic here.  Or maybe there is a 
simpler way of doing this?


(I'm using sqlalchemy 0.7)

Thanks a lot,
  Andre

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/3XisZc_YH9IJ.
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