Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] SQL Alchemy pickle not dumping properly #394

Open
Eyon42 opened this issue Jun 12, 2021 · 0 comments
Open

[Bug] SQL Alchemy pickle not dumping properly #394

Eyon42 opened this issue Jun 12, 2021 · 0 comments

Comments

@Eyon42
Copy link

Eyon42 commented Jun 12, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant