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

Issue with association object with primary key composed of two foreign keys

Open eino opened this issue 5 years ago • 0 comments

Hi,

I have an issue with my data structure. Not sure if it's a bug, or (more probably), something that could be solved by tweaking the shema.

I have two objects, let's call them A and B, and an assotiation object A_to_B with a quantity. This association object's primary key is composed of the foreign keys of A and B

We receive data as nested : A containing A_to_B containing B :

"a" : {
"id": a_id,
// other A data

b_data: {
quantity: 1,
b: {
"id": b_id,
// B data
}
}

The first time I load the data, everything works fine. But the issue is that when trying to re-load existing data, sqlalchemy tries to delete the existing A_to_B (and to recreate it afterward).

It seems for some reason, marshmallow-sqlalchemy doesn't realize the association object is the same than the one that exists in the base. Also at time of merge, sqlalchemy doesn't realize either, and removes the old instance to re-create the same thing.

For now the association schema looks like this :

class AToBSchema(BaseSchema):
    b = Nested(BSchema)
    class Meta(BaseSchema.Meta):
        model = AToB

Any idea to prevent this unnecessary new instance ? (with a deletion of the old one)

eino avatar Dec 16 '19 11:12 eino