我正在用sqlalchemyorm编写我的第一个大型应用程序,使用SQLite作为数据库引擎。 我在表项和附件之间有一对多的关系。相关代码为:
class Item(Base):
__tablename__ = 'items'
__table_args__ = ({'keep_existing': True})
item_id = Column(Integer, primary_key=True)
title = Column(String(255), nullable=False, unique=True, index=True)
description = Column(types.Text, nullable=False)
# ... other attributes skipped
# one-to-many bidirectional relationship to Attachments
attachments = relationship('Attachment', backref='item')
def __repr__(self):
return "Item(%s)" % self.title
class Attachment(Base):
__tablename__ = 'attachments'
__table_args__ = ({'keep_existing': True})
attachment_id = Column(Integer, primary_key=True)
item_id = Column(Integer, ForeignKey('items.item_id'))
name = Column(String(255))
filetype = Column(String(32))
description = Column(String(255))
data_blobb = deferred(Column(types.LargeBinary))
modification_date = Column(types.DateTime, nullable=False,
server_default=text('CURRENT_TIMESTAMP'))
def __repr__(self):
return "Attachment(%s.%s: %s)" % (self.name, self.filetype, self.description)
尝试执行以下简单查询时出现问题:
^{pr2}$当程序要打印时res.附件,它将生成一个包含BLOB列的SELECT语句,并与已知的
UnicodeEncodeError: 'ascii' codec can't encode character u'\xda' in position 267: ordinal not in range(128)
在基于sqlite3的Python脚本中执行相同的SELECT时,我可以通过对返回的内容调用.encode('utf-8')
来避免异常。在
我也尝试了coercing检索到的内容,但没有成功。 我应该怎么做才能使用SQLAlchemy访问上述列?在
目前没有回答
相关问题 更多 >
编程相关推荐