如何在SQLAlchemy中将字节写入Blob

时间:2019-06-18 03:52:02

标签: python mysql python-3.x sqlalchemy protocol-buffers

我有以下表格定义:

const variables = {
  license: {
    licenseId: 123,
    // and so on
  }
}
this.apollo.mutate({ mutation, variables })

现在我有个原型:

class UserExtensibleData(CommonDeclarativeBase.DECLARATIVE_BASE):
    __tablename__ = 'user_extensible_data'
    id = Column(BIGINT(unsigned=True), primary_key=True, nullable=False)
    user_id = Column(BIGINT(unsigned=True), ForeignKey(User.id), nullable=False, unique=True)
    # using _blob so client is aware that it is a blob, since UserPreferences is a proto object
    user_preference_blob = Column(BLOB, nullable=True)
    unix_timestamp_last_update_seconds = Column(INTEGER, nullable=False)

    def __init__(self, user_id: int, user_preference_blob, unix_timestamp_last_update_seconds: int):
        self.user_id = user_id
        self.user_preference_blob = user_preference_blob
        self.unix_timestamp_last_update_seconds = unix_timestamp_last_update_seconds

我的代码是:

message UserPreferences {
  int64 unix_time_last_update = 5;
}

保存正常,没有错误。但是,当我尝试取回结果时,出现错误:

extensible_user_data = session.query(UserExtensibleData).filter(UserExtensibleData.user_id == user_id).first()
        byte_array = user_preferences.SerializeToString()
        session.add(extensible_user_data)
        extensible_user_data.user_preference_blob = byte_array
        extensible_user_data.unix_timestamp_last_update_seconds = self.datetime_processor.get_utc_timestamp_now()
        session.commit()
        return user_preferences

产生错误:

return session.query(UserExtensibleData).filter(UserExtensibleData.user_id == user_id).first()

让我困惑的是为什么它首先尝试解码...似乎我只是在写字节,而SqlAlchemy应该只是在读字节。有谁知道这个问题可能是什么?

0 个答案:

没有答案