1
我想從SQLite中使用Flask-SQLAlchemy讀取BLOB(LargeBinary)對象,並且出現此錯誤:TypeError: must be string or read-only buffer, not None
。從SQLite通過Flask讀取BLOB(LargeBinary)SQLAlchemy返回無
我的代碼如下:
@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)
而且我SQLAlchemy的模式是:
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)
這裏是我的架構:
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)
);
該錯誤發生在哪條線上?這聽起來像你的查詢返回'None'。 – gregb212
這絕對不是'None',因爲我可以在調用'job.compressed_csv'之前將'job.record_count'轉儲到控制檯。 –