1

I am trying to read a BLOB (LargeBinary) object from SQLite using Flask-SQLAlchemy, and I'm getting this error: TypeError: must be string or read-only buffer, not None.

My code is as follows:

@app.route('/csv/<int:job_id>', methods=['GET'])
def get_csv(job_id):
    """ Action to retrieve the compressed CSV from the database """
    job = db.session.query(Job).filter_by(id=job_id).first()
    csv = decompress(job.compressed_csv)
    sio = StringIO()
    sio.write(csv)
    sio.seek(0)
    return send_file(sio,
                     attachment_filename=
                     "{}_{}.txt".format(job_id, time.strftime("%Y%m%d%H%M%S")),
                     as_attachment=True)

And my SQLAlchemy model is:

class Job(db.Model):
    """ SQLAlchemy Job Model """
    id = db.Column(db.Integer, primary_key=True)
    list_type_id = db.Column(db.Integer, db.ForeignKey('list_type.id'),
                             nullable=False)
    list_type = db.relationship('ListType',
                                backref=db.backref('jobs', lazy='dynamic'))
    record_count = db.Column(db.Integer, nullable=False)
    status = db.Column(db.Integer, nullable=False)
    sf_job_id = db.Column(db.Integer, nullable=True)
    created_at = db.Column(db.DateTime, nullable=False)
    compressed_csv = db.Column(db.LargeBinary)

    def __init__(self, list_type_id, record_count=0, status=0, sf_job_id=0,
                 csv=None, created_at=datetime.utcnow()):
        self.list_type_id = list_type_id
        self.created_at = created_at
        self.record_count = record_count
        self.status = status
        self.sf_job_id = sf_job_id
        self.compressed_csv = csv

    def __repr__(self):
        return '<Job {}>'.format(self.id)

Here is my schema:

CREATE TABLE alembic_version (
    version_num VARCHAR(32) NOT NULL
);
CREATE TABLE job (
    id INTEGER NOT NULL, 
    list_type_id INTEGER NOT NULL, 
    record_count INTEGER NOT NULL, 
    status INTEGER NOT NULL, 
    sf_job_id INTEGER NOT NULL, 
    created_at DATETIME NOT NULL, 
    compressed_csv BLOB, 
    PRIMARY KEY (id), 
    FOREIGN KEY(list_type_id) REFERENCES list_type (id)
);
CREATE TABLE list_type (
    id INTEGER NOT NULL, 
    name VARCHAR(80) NOT NULL, 
    PRIMARY KEY (id), 
    UNIQUE (name)
);
2
  • What line did that error occur on? It sounds like your query is returning None. Commented Oct 15, 2013 at 14:41
  • It's definitely not None because I can dump job.record_count to the console before I call job.compressed_csv. Commented Oct 15, 2013 at 15:07

1 Answer 1

2

So it turns out that I was inserting the data using csv = buffer(compress(csv)). This resulted in storing NULL for that field. Removing the buffer function solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.