marshmallow-sqlalchemy icon indicating copy to clipboard operation
marshmallow-sqlalchemy copied to clipboard

[Bug] SQL Alchemy pickle not dumping properly

Open Eyon42 opened this issue 4 years ago • 0 comments

I have a SQLA model:

class Model(db.Model):
    __tablename__ = "table"
    id = Column(Integer, primary_key=True
    data = Column(PickleType, nullable=False)

Then auto create a Marshmallow Schema:

class VerificationSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = VerificationData
        include_fk = True

I tested with a @pre_dump method, i got the Model instance and was able to access Model.data as a python dict. Then i used @post_dump, but then data["data"] was a malformed JSON string (it used single quotes).

So, the problem is inside how marshmallow-sqlalchemy transforms the SQLA object.

For now i solved it adding a @post_dump method, this gives me the data["data"] as a python dict.

class VerificationSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = VerificationData
        include_fk = True

    @decorators.post_dump
    def deserialize_pickle_bin(self, data, **kwargs):
        # For some reason, marshmallow serializes the dict to a string.
        # So this is nesessary.
        data["data"] = json.loads(data["data"].replace("'", "\""))
        return data

But I think this should be default behaviour.

Eyon42 avatar Jun 12 '21 04:06 Eyon42